开发者

How to do animation using swing and clojure?

开发者 https://www.devze.com 2023-01-01 07:05 出处:网络
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

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?


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.


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.


you can use Trident library : Here


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消