I'm making a gui for my chess game, and I want the pieces (images) to have their movements animated. I would like it to be a smooth animation, so that it starts out slow, speeds up, and then slows down to a stop at the end-point. Right now, I have a JFrame with a GridLayout for my chess board, and images of pieces in the grid. Is it possible to do this without too much trouble? Would I have to look into something like openGL (never used it before)?
1 Answers
You don't need OpenGL for this as basic Swing animation will handle this nicely. Simply elevated the JLabel-holding chess piece from the GridLayout-using container to the glasspane, animate it on the glasspane so that it moves over the destination container and then set it down from the glasspane onto the destination container (probably your GridLayout holds JPanels, so the destination will be a JPanel). A Swing Timer ought to work well to run your animation loop.
So more precisely, your logic could be something like:
- user clicks on chess piece, and this piece's reference is stored in a variable
- user clicks on destination location.
- Chess logic engine checks to make sure that this is the user's actual turn and that this is a valid move
- Disable the user's ability to make any moves while animation is occurring (turn off MouseListener)
- remove piece JLabel from its current JPanel container and place at same screen location on the glasspane
- Start Swing Timer to do animation, moving from location of current container to new location.
- Timer will call setLocation on the piece moving it from A to B
- After timer complete, remove piece from the glasspane and place into new container
- Tell chess engine OK for next player to move.
Having said all this, my own preference is to simply allow the user to drag the piece JLabel from source to destination and have his dragging be the animation. I have code for that shows how to do this somewhere on this very site. To me this is a more natural way for the user to move the chess piece. Hang on, I'll search for my link.
The link is here: does-adding-a-jlabel-to-a-jpanel-hide-the-jpanel

- 1
- 1

- 283,665
- 25
- 256
- 373
-
Thanks. The dragging actually does make more sense, I think I'll do that for the human player. But when the computer makes a move I will need to animate it. As for your animation solution, would they be rigid movements? Or is there a way to make them start slow, speed up, and then slow down to a stop? I would be ok with rigid movements, I'm just curious. – Tim May 24 '12 at 02:44
-
@Tim: OK, that makes sense re the computer's move being animated. You could easily start slow and then speed up, no problem. You would first need to decide which to vary, speed or animation step size -- I'd choose the latter. Next you would need to calculate the starting and ending location, then you could calculate the step sizes for the animation, small steps at the beginning and end, larger in the middle, create an `ArrayList
` of the locations relative to the glasspane, and then have the Timer iterate through points in the list as it's running. – Hovercraft Full Of Eels May 24 '12 at 02:47