本文介绍了动态图形编程(动画)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图code一个简单的动画就像一个移动的圆圈。我一直在使用的getGraphics()试图
并与工作,但它不是动态的,它画了只是一个时间
I am trying to code a simple animation like a moving circle. I have tried using getGraphics()
and work with that but it's not dynamic and it's painted for just one time
所以,请帮助我,指导我code动态图形程序。
So please help me and guide me to code a dynamic graphic program.
我的意思是比如定义一个函数,并在每次开机时调用的时候,它吸引的标签上一条线。
I mean for example defining a function and every time when it called, it draws a line on a label.
推荐答案
下面是如何使越来越多的矩形:
Here is how to make a growing rectangle:
public class MovingRectangle extends JPanel {
private Timer timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent event) {
rectWidth += 100;
repaint();
}
};
private int rectWidth = 100;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(0, 0, 100. rectWidth);
}
public void start() {
timer.start();
}
public void stop() {
timer.stop();
}
public void reset() {
rectWidth = 100;
repaint();
}
}
这篇关于动态图形编程(动画)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!