6

我正在尝试在 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.");
            }
        }
    }
} 
4

2 回答 2

11

有几件事很突出...

问题”

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

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

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

建议

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

作为旁注,也不鼓励覆盖paint顶级容器的方法,例如Applet. 顶级容器往往包含许多额外的组件,通过覆盖这样的paint方法,您会破坏此设置。此外,顶级容器也不倾向于双缓冲。

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

在此处输入图像描述

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

        }

    }

}
于 2013-01-21T10:09:01.437 回答
3

您需要替换g.drawImage(img2, 200, 200, this);g.drawImage(img2, x, y, this);

于 2013-01-21T07:27:06.783 回答