如何移动图像(动画)? [英] How to move an image (animation)?
问题描述
我正在尝试在 x 轴上移动船(还没有键盘).如何将运动/动画与 boat.png
而不是任何其他图像相关联?
I'm trying to move the boat on the x-axis (without the keyboard yet). How can I relate the movement/ animation to the boat.png
and not to any other image?
public class Mama extends Applet implements Runnable {
int width, height;
int x = 200;
int y = 200;
int dx = 1;
Image img1, img2, img3, img4;
@Override
public void init(){
setSize(627, 373);
Thread t = new Thread(this);
img1 = getImage(getCodeBase(),"Background.png");
img2 = getImage(getCodeBase(), "boat.png");
img3 = getImage(getCodeBase(), "LeftPalm.png");
img4 = getImage(getCodeBase(), "RightPalm.png");
}
@Override
public void paint(Graphics g){
g.drawImage(img1, 0, 0, this);
g.drawImage(img2, 200, 200, this);
g.drawImage(img3, 40, 100, this);
g.drawImage(img4, 450, 130, this);
}
@Override
public void run() {
while(true){
x += dx;
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
System.out.println("Thread generates an error.");
}
}
}
}
推荐答案
有一些突出的地方...
There are a few things that stand out...
如前所述,您需要为图像绘制过程提供可变参数.g.drawImage(img2, x, y, this);
,这将允许您定义应该绘制图像的位置.
As has already been stated, you need to supply variable arguments to the image drawing process. g.drawImage(img2, x, y, this);
, this will allow you define where the image should be painted.
虽然您已经实现了 Runnable
,但实际上您还没有启动任何线程来调用它.这意味着,实际上并没有改变变量.
While you've implemented Runnable
, you've actually not started any threads to call it. This means, nothing is actually changing the variables.
在你的 start
方法中,你应该调用类似 new Thread(this).start()
的东西.
In you start
method, you should be calling something like new Thread(this).start()
.
尽管您已将问题标记为 Swing,但您使用的是 AWT 组件.不推荐这样做(实际上,通常不建议使用小程序,因为它们很麻烦 - 恕我直言).您很快就会发现的另一个问题是它们不是双缓冲的,这通常会导致在执行动画时出现闪烁,这是不可取的.
Although you've tagged the question as Swing, you're using AWT components. This isn't recommended (in fact applets are generally discouraged as they are troublesome - IMHO). The other problem, as you are bound to find out shortly, is they are not double buffered, this generally leads to flickering when performing animation, which isn't desirable.
作为旁注,也不鼓励覆盖诸如 Applet
之类的顶级容器的 paint
方法.顶级容器往往包含一些额外的组件,通过像这样覆盖 paint
方法,你会破坏这个设置.此外,顶级容器也不倾向于双缓冲.
As a side note, it is also discouraged to override the paint
method of top level containers like Applet
. Top level containers tend to contain a number additional components, by overriding the paint
method like this, you destroy this setup. Also, top level containers don't tend to be double buffered either.
下面的示例使用了 JFrame
,但将其转换为使用 JApplet
不需要太多(只需删除 AnimationPanel
关于它.这是通常不鼓励从顶级容器扩展的另一个原因;)
The example below uses a JFrame
, but it wouldn't take much to convert it to use a JApplet
(just drop the AnimationPanel
on to it. This is another reason why extending from top level containers is generally discouraged ;)
public class AnimatedBoat {
public static void main(String[] args) {
new AnimatedBoat();
}
public AnimatedBoat() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new AnimationPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class AnimationPane extends JPanel {
private BufferedImage boat;
private int xPos = 0;
private int direction = 1;
public AnimationPane() {
try {
boat = ImageIO.read(new File("boat.png"));
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + boat.getWidth() > getWidth()) {
xPos = getWidth() - boat.getWidth();
direction *= -1;
} else if (xPos < 0) {
xPos = 0;
direction *= -1;
}
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return boat == null ? super.getPreferredSize() : new Dimension(boat.getWidth() * 4, boat.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int y = getHeight() - boat.getHeight();
g.drawImage(boat, xPos, y, this);
}
}
}
这篇关于如何移动图像(动画)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!