harrie harrison wrote:I'm trying to make a 10x10 Draughts board for my application.
An application that loads and visualizes this notation by putting the pieces on the correct location of the board.
example FEN notations
(W white pieces :B black pieces , K in front of number = KING piece the numbers are the black squares (1-50) where the piece has to be placed)
Now I am a java novice so I could use some pointers as in how to proceed at making the board.
My plan was to add 100 jPanels to jPanel chessBoard. And then use a % algorithm to fill the right squares with a black or white ImageIcon. (the assignment requires me to use these).
Would this be a viable approach?
Well, in my chess program my board is a single JPanel, and I move pieces by using a mouseListener on the panel. in mousePressed, e.getX() and e.getY() can be transformed via formula to algebraic chess notation, likewise for mouseReleased.
for drawing the pieces, my Board class has a paint routine that queries an 8x8 char array representing the position to determine what piece to draw, and the same formula I decsribed for my mousePressed and mouseReleased is more or less used in reverse to tell paint where to draw the piece.
Hope that helps, any more questions I'm happy to help out.
Joined: Jun 18, 2009
This is my first version of the DraughtsBoard. (code below) I used Jlabels with imageicons. This is not optional, i have to use the square .gif icons provided with the assignment.
I wanted to try another solution to see if that was more workable with mouselisteners, this JLabel version gave me trouble with it. (note im not very good with java.. yet)
So if i could get some advice what would be the easiest solution that'd be great.
Also with this 10x10 grid how can i pinpoint which jLabel i have selected so the piece will be put on the correct spot. I'm sorry if this is a stupid question but as i said im still learning.
Joined: May 13, 2009
ok p.s. I did not notice that this was an assignment, and that you had to use imageicons. I have no idea how I would implement 100 imageicons on a single panel. Maybe a GridLayout manager? ANother possibility is to have the mouseListener on the underlying panel of JFRame rather than a mouseListener for each square. e.getX and e.getY could be used to determine which square is being clicked on.
ANyways, I'm not an expert or anything, this chess program was basically a learning project that has undergone a couple of rewrites. my current solution is based on the following, which in retrospect probably doesn't fit well with your requirements.
1. instead of using ImageIcons for the pieces, I use a combination of graphics commands such as fillArc, fillOval, etc. This makes the idea of using just a single JPanel for the entire board easier to implement.
2. Any time the position changes, the entire board is repainted. I'm not sure this is necessary, but it is simpler to implement. Intuitively and esthetically I'm not thrilled about redrawing the entire board for each move, but it works fine and does not seem to cause any resource issues.
Anyways, first time around I had the same idea as you, ImageIcons and JLabels and separate JPanels for each square, but I was not thrilled with the idea of 64 JPanels. That's just me, I can't really say that my idea is better or worse than yours, I'm sure you can make yours work ok. Does that answer your question?
My program is available online at the link in my signature. You will find there a technical overview of the design that you may benefit from.
Anyways, I enjoy this kind of stuff, so we can discuss more if you like. Do you intend to program your own draughts "engine" to play against? That will be a challenge. A proper engine would use some sort of recursive algorithm to think ahead, and I find that sort of stuff very difficult to wrap my head around.
p.s. if you edit your post to reduce the indents in your code, that should eliminate the need to scroll horizontally to read the posts in the thread.
p.s.s. here is my paint method, which should give you an idea of how the board is set up.
Joined: Jun 18, 2009
Thanks for your tips, but I really have to use those .gifs for the pieces and squares provided. As i said its part of the assignment.
So I'm gonna have to stick with JLabels
And my board wont be a game yet, its just a FEN viewer for now. An application that can read FEN notations from a file and loads those into diagrams.
I might turn it into a real game when I am ready for it but that time isnt here yet ;D.
Your game is nice i think, making algorithms for moves seems very hard to me. So for a few weeks work its quite nice already.
Anyway if anyone has some practical tips its greatly appreciated.
Joined: May 13, 2009
yeah, I didn't notice you had to use the gifs until later. I have updated my previous post with a couple of ideas
p.s. not sure where you got the idea of a couple of weeks work. It took me much longer to figure all that stuff out.