gpt4 book ai didi

java - 如何移动图像(动画)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:49:13 25 4
gpt4 key购买 nike

我正在尝试在 x 轴上移动船(还没有键盘)。如何将运动/动画与 boat.png 相关联,而不是与任何其他图像相关联?

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.");
}
}
}
}

最佳答案

有几件事很突出......

“问题”

如前所述,您需要为图像绘制过程提供可变参数。 g.drawImage(img2, x, y, this);,这将允许您定义图像的绘制位置。

虽然您已经实现了 Runnable,但您实际上并没有启动任何线程来调用它。这意味着实际上没有任何改变变量。

在您的start 方法中,您应该调用类似new Thread(this).start() 的方法。

建议

尽管您已将问题标记为 Swing,但您使用的是 AWT 组件。不推荐这样做(事实上,通常不鼓励使用 applet,因为它们很麻烦 - 恕我直言)。另一个问题,您很快就会发现,它们不是双缓冲的,这通常会导致在执行动画时出现闪烁,这是不可取的。

作为旁注,也不鼓励覆盖顶级容器(如 Applet)的 paint 方法。顶层容器往往包含许多附加组件,通过像这样重写 paint 方法,您会破坏此设置。此外,顶级容器也不会采用双缓冲。

下面的示例使用了一个 JFrame,但是将它转换为使用一个 JApplet 并不需要太多(只需删除 AnimationPanel在上面。这是为什么通常不鼓励从顶级容器扩展的另一个原因;)

enter image description here

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);

}

}

}

关于java - 如何移动图像(动画)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com