我正在尝试制作一个程序,它可以像简单的赛马一样运行,但是我不能设定不同的速度。就我所见,当计时器得到不同的值时,例如:我有两张图片,一张是计时器40,另一张是80,它们以相同的速度移动,但是一张有80的图片会被切碎,或者说时钟更少,而这张我有40的图片会更平滑。
public class AnimatedThing {
public AnimatedThing() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Trke");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 600));
frame.setLayout(new BorderLayout());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setLayout(new GridLayout(4, 1));
frame.setBackground(Color.white);
enemyRed enemyPink = new enemyPink(); // sub-panel 1
frame.add(enemyPink);
frame.setLocationRelativeTo(null);
}
});
}
public class enemyPink extends JPanel {
private BufferedImage enemyPink;
public enemyPink() {
try {
enemyPink = ImageIO.read(new File("C:\\Users\\SMRTNIK\\Documents\\NetBeansProjects\\Sistem elektronske biblioteke - SEB\\images\\enemy - pink.png"));
Timer timer2 = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + enemyPink.getWidth() > getWidth()) {
} else {
repaint();
}
}
});
timer2.setRepeats(true);
timer2.setCoalesce(true);
timer2.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return enemyPink == null ? super.getPreferredSize() : new Dimension(enemyPink.getWidth() * 4, enemyPink.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(enemyPink, xPos, 0, this);
}
}
1条答案
按热度按时间bvjveswy1#
最好先阅读一下标准文档,然后看一些关于如何操作的教程。代码非常混乱,我无法解决它。我将发布一个链接:https://youtu.be/kmgo00avvew