views:

146

answers:

5

I'm trying to animate a chess piece in a board. First I created a java.util.Timer object that "scheduleAtFixedRate" a TimerTask implemented as a proxy function. So I kept a record of the piece to move (piece-moving-record) and when it's apropriate (when the user move the piece using the mouse) the TimerTask proxy function should be test if the record is not nil and execute the piece-moving function. The piece-moving function just updates the x and y coordinates of the piece, according to a vector pre-calculated. I put a add-watch on the piece-moving-record so when it changes it should repaint the board (canvas). The paint method tests if this piece-moving-record is not nil to paint it.

The problem is that the animation doesn't appear. The piece just jump to the destiny, without the movement between. There is some problem with the animation scheme ou there is a better way to do it?

+1  A: 

Swing doesn't do animation automagically it does not matter if you have a timer or not. If you want to animate movement you need to move the piece some distance at a time and repaint the canvas with every move so it gives the illusion so to speak of movement.

Hamza Yerlikaya
That's exactly what I'm doing, when the piece moves the repaint method of canvas is called through add-watch.
Humberto Pinheiro
A: 

There's a Animator demo in the JDK. It uses awt but it should give you some ideas.

jdk1.6.0_20\demo\applets\Animator

Guy
A: 

you can use Trident library : Here

Dimitri
Should try, clojure with swing seems hard to me.
Humberto Pinheiro
+1  A: 

Updates to Swing components should be done on the Event Dispatch Thread (EDT). So you should be usig a Swing Timer since any code executed when the Timer fires will be executed on the EDT.

All you should have to do is set the location of the chess piece and it will repaint itself automatiocally.

Read the section from the Swing tutorial on How to Use Timers for more information.

camickr
You're right, the move now works but still is not smooth enough, would check swing timers more. Thanks for you answer.
Humberto Pinheiro
A: 

Another idea might be to look at the Processing visualisation framework from processing.org. It's a simple but powerful and well-documented programming language for animation and visualisation. The API is very straightforward , and has a very transparent Clojure wrapper named Rosado.

NielsK