如何使用摆动计时器启动/停止动画 [英] how to use a swing timer to start/stop animation
问题描述
有人能教我如何使用摇摆定时器
有以下目的:
我需要有一个开始进行动画(简单的动画,如旋转),当我点击鼠标多边形;停止动画,当我再次点击。
我没有理解的MouseListener
的作品,但与实际动画的方式问题。我试图与的paint()
方法,在那里我会画画,擦除和重绘多边形(模拟例如旋转)内,而块模拟动画,但内里的同时,该小程序将不听点击。只有一段时间后,会听。我需要摆动计时器打破,而当我点击鼠标。
进口javax.swing.Timer中;
添加属性;
定时器定时器;
布尔B: //启动和回采动画
添加以下code到框架的构造。
定时器=新定时器(100,新的ActionListener(){
@覆盖
公共无效的actionPerformed(ActionEvent的AE){
//改变多边形数据
// ... 重绘();
}
});
覆盖涂料(图形G)
,并从由的actionPerformed修改的数据绘制多边形(E)
。
最后,启动/停止动画按钮,在其事件处理程序如下code。
如果(B){
timer.start();
}其他{
timer.stop();
}
B =!;
Could someone teach me how to use a swing timer
with the following purpose:
I need to have a polygon that begins being animated(simple animation such as rotating) when I click the mouse; and stops animating when I click again.
I do not have problems understanding the way the MouseListener
works, but with the actual animation. I tried simulating the animation with a while block inside the paint()
method where I would draw, erase and redraw the polygon(to simulate a rotation for example), but inside the while, the applet would not listen to the clicks. It would listen only after the while. I would need the swing timer to break the while when I click the mouse.
import javax.swing.Timer;
Add an attribute;
Timer timer;
boolean b; // for starting and stoping animation
Add the following code to frame's constructor.
timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
// change polygon data
// ...
repaint();
}
});
Override paint(Graphics g)
and draw polygon from the data that was modified by actionPerformed(e)
.
Finally, a button that start/stop animation has the following code in its event handler.
if (b) {
timer.start();
} else {
timer.stop();
}
b = !b;
这篇关于如何使用摆动计时器启动/停止动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!