[Java]利用javax.swing.Timer类在窗口上实现动画效果

  javax.swing.Timer类在创建时需要指定时间间隔和定时器到时间需要执行的动作,即ActionListener。

1 Timer timer = new Timer(100, taskPerformer);
2 timer.start();

  动画效果需要有连续的图画作为动作的播出效果,然后利用index = (index + 1)%imageCounts;来实现循环播放的效果。例如,

1 private ActionListener taskPerformer = new ActionListener() {
2         @Override
3         public void actionPerformed(ActionEvent e) {
4             currentImageIndex = (currentImageIndex + 1)
5                     % TankWarConfigure.explosionCnt;
6             exp.repaint();
7         }
8     };

  完整例子代码:链接: http://pan.baidu.com/s/1jGzDalw 密码: pcr6

posted @ 2014-03-14 19:08  life91  阅读(1725)  评论(0编辑  收藏  举报