Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chess game using Java AWT/ Swing

 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm supposed to be creating a chess game using Java AWT/Swing. Unlike the typical 8x8 board, mine is supposed to be octagonal shaped.  I've read certain forums which state I'm supposed to create the board first using something called GridLayout before creating the pieces. Are there any additional things I should know before starting with this?
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is an octagonal chessboard?

Given that, you have many options to place your pieces on a chessboard. For instance, I use a GridLayout with JLabels as fields, and I put my pieces (png files) as icons of these JLabels. These JLabels are opaque, so it is easy to choose other colors for the fields, or to swap white and black.
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never heard of an octagonal chessboard either, but it seems likely that not all of its cells can be squares. In which case a GridLayout, or any layout I know of, would have trouble rendering it.

However maybe it's some aggregration of square cells which fit inside an octagonal border. Who knows? It doesn't make sense to start designing code to display some particular view when you have no idea what the view looks like, so the first thing to do would be to find that out.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the chess board that I am trying to recreate. Notice that each player only has 1 knight, 1 castle, and 1 bishop unlike the ordinary chess where each player has a pair of those. There is also only 1 pawn instead of the typical 8 pawns.

 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, okay. So your requirements do specify a grid of squares, which means that a GridLayout would be a suitable tool.

As for what you should know before starting out with a GridLayout, well... There's a lot you should know. Basic Swing programming would be helpful, for example. Basic Java programming, too. But since we have no idea what your abilities are, it's a bit hard to be specific.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a few questions that I would like to clarify regarding GridLayout. So I created GridLayout(8,8) and then I inserted JButtons. What I don't understand is why I would need the chessboard to act like buttons, when the only thing I need to click on are the chess pieces? How is JPanel related to GridLayout when creating a chess board? Also what does extend mean in public class ChessBoard extends JPanel? When do I need to include it? Sometimes there's the word implement too. Eg. implements MouseListener.
 
Campbell Ritchie
Sheriff
Posts: 51415
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you actually have chessman objects you can click on? It would be customary to click on the square and that activates the man on that square.
As for questions about extends and implements, those are basic concepts which you will find in the Java™ Tutorials and just about any Java® textbook. You shou‍ld understand those before you try anything advanced.
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems easiest to give your panel a GridLayout(11, 11). I created a dedicated class for my chess squares, namely Square extends JLabel. As icon of such a Square I use one of my chess pieces, if any. Each Square has two integers, one for the rank and one for the file, and to each Square I added just one mouseListener. When clicked on a Square, the mousListener then simply asks that Square for its rank and file, and then transfers that data to the game enigine for further processing.

When filling the Grid with your Squares, in each corner make the six Squares fully transparant. That will give you the octagonal board in a very easy way.

But I must agree with Campbell: if terms like 'extends' and 'implements' mean nothing to you, do you think you are up to writing a chess program with GUI in Java?
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh well, I don't have a choice. It's my assignment. Whether I know it or not, I'll have to learn it up within this one month.
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see... an interesting assignment, but not easy. Can you tell us more details about this assignment? What is this chess engine supposed to be able of, and have you an idea about how your gui should operate?
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically I'm supposed to create this chessboard in 3D(to get more marks,but a bit more farfetched don't you think?)/ 2D. The pieces should be able to make a legal move. But since it's octagonal shaped I also have to consider whether the cell exists or not. So I can't just make a knight go in an L shaped. I need to make sure the L shape goes over existing cells. The pieces can be moved using drag and drop and if I capture a piece, it should be removed from the board. Check and checkmate should work(and there should be background music/sound effect/short movie played when this happens.) It's supposed to be played between two players so I don't actually have to do player vs computer.
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3d?  Music? Whoever gave you that assignment must have a great sense of humor... and all that within a month, well, no time to waste!

As said, I would use an 11x11 chessboard, with the corner squares marked as invalid. Should not be a problem. One advantage: with only two pawn, miles apart, you probably won't be suffering from en passant problems.

Well, let us know if you have questions or problems. Success and enjoy!
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the board I created so far.


But it's behind the toolbar instead of above it. Another thing is that the board covers the entire frame. I want the panel to be smaller than the actual frame.



I have yet to find a way to make the corners transparent yet. Still working on it.
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started from the size of my hand made chess pieces. These measured 40 x 40 pixels, with transparency. So I made my Squares also 40 x 40, and an 11 x 11 board takes up 440 x 440. You could then set the preferred size of the panel to that dimension. Make that panel part of the containing frame.
A label can be made fully transparent by using the method setOpaque(false). All the labels that are part of the chessboard you would set the opaque property to true, and give each the correct background color.
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the current chess board based on the algorithm I have created. However it is not octagonal. Any suggestions for a new algorithm?



 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another problem is that the buttons that are not opaque can still be pressed. Does this method "getAccessibleContext()" help to make the JButton invalid?
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simple straightforward way is to create a List<Point> that contains the coordinates of all illegal squares. For instance

and check in you nested for loop if an i, j combination is present in that List. For a Point the 'equals' method is already nicely redefined.
 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nadeera rajapakse wrote:Another problem is that the buttons that are not opaque can still be pressed. Does this method "getAccessibleContext()" help to make the JButton invalid?

A JLabel is much handier. Is there any particular reason why you would use a JButton?
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The list method doesn't bring any effect. Am I using it incorrectly? Here's what I have so far with JButton implementation.

 
Piet Souris
Rancher
Posts: 1529
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not actually using this illegalFields list. I had this in mind (sorry for giving a bit of code, but I hope you get what I had in mind)

hen, I create a dedicated inner Square class, like this

and finally I created the JPanel chessBoard, like
 
nadeera rajapakse
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:You are not actually using this illegalFields list. I had this in mind (sorry for giving a bit of code, but I hope you get what I had in mind)


Thank you, for giving me that bit of code!!! I'll work on this first for now, and get back to you if I have any other questions. 
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic