我应该使用哪个类来设置一组帧的动画?我确实喜欢这个。它工作得很好。那就是我可以为图片制作动画。哪个班级最好?
public JLabel frame;
public Image[] frames;
public Bird() {
frames = new Image[5];
frames[0] = getToolkit().getImage("Resources\\bird_swing_up2.png");
frames[1] = getToolkit().getImage("Resources\\bird_swing_up.png");
frames[2] = getToolkit().getImage("Resources\\bird_swing_down.png");
frames[3] = getToolkit().getImage("Resources\\bird_swing_up.png");
frames[4] = getToolkit().getImage("Resources\\bird_swing_up2.png");
}
我在另一个类中为此设置动画?这样可以吗?这种方法更好吗?
private Image city;
private Image ground;
private int x = 0;
private Timer speedController;
private Bird bird;
private Timer timer;
int i = 0;
public Board() {
super();
ground = getToolkit().getImage("Resources\\ground.png");
city = getToolkit().getImage("Resources\\city.png");
speedController = new Timer(5, new GroundSpeed());
speedController.start();
bird = new Bird();
timer = new Timer(50, new Fly());
timer.start();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(city, 0, 0, 1082, 450, this);
g.drawImage(ground, x, 372, this);
g.drawImage(ground, x + 350, 372, this);
g.drawImage(bird.frames[i], 100, 100, this);
if(x == -350){
x = 0;
}
}
public class GroundSpeed implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
x--;
repaint();
}
}
public class Fly implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
//frame = new JLabel(frames[i]);
i++;
if(i == 4){
i = 0;
}
repaint();
}
}
答案 0 :(得分:0)
嗯..不幸的是, Java中没有神奇的类来做到这一点,这意味着你必须编写自己的动画机制。
创建动画有两种主要技巧:
基于Timee的动画总是更受欢迎!关于这个主题的最好的东西之一是available here。它共享了 jsFiddle 示例,演示了帖子中解释的每种技术。
不久前I translated that code to C++/Qt: