1

我对 Java 编程相当陌生,我正在开发我的第一个 2D 游戏。为了弄清楚如何让我的播放器使用 WASD 键移动,我遵循了 Oracle 关于 Key Listeners 的教程之一。该示例已经有一个鼠标侦听器。我决定删除鼠标监听器并添加一个 KeyListener。我为 D 添加了一个 KeyListener ,它将我的图形向右移动。但是,我的图形只向右移动 1 个坐标,而不是更新正方形的新位置,并在按下 D 键时连续循环移动它。我正在使用绘画组件来绘制我的图像。

这是我的课:

import javax.swing.SwingUtilities;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class PaintComponent {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        System.out.println("Created GUI on EDT? "
                + SwingUtilities.isEventDispatchThread());
        JFrame f = new JFrame("Swing Paint Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MyPanel());
        f.pack();
        f.setVisible(true);
    }

}

class MyPanel extends JPanel {

    private int squareX = 50;
    private int squareY = 50;
    private int squareW = 20;
    private int squareH = 20;
    public int posX = squareX;
    public int posY = squareY;

    public MyPanel() {
        setFocusable(true);
        requestFocusInWindow();

        setBorder(BorderFactory.createLineBorder(Color.black));

        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                int keyCode = e.getKeyCode();

                if (keyCode == KeyEvent.VK_D) {

                    moveSquareRight(posX);

                } else {
                    e.consume();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                int keyCode = e.getKeyCode();
                e.consume();

            }
        });
        /*
         * addMouseListener(new MouseAdapter() { public void
         * mousePressed(MouseEvent e) { moveSquare(e.getX(),e.getY()); } });
         * 
         * addMouseMotionListener(new MouseAdapter() { public void
         * mouseDragged(MouseEvent e) { moveSquare(e.getX(),e.getY()); } });
         */

    }

    private void moveSquareRight(int x) {
        int OFFSET = 1;

            if (squareX == x) {

                repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
                squareX = x + 1;
                repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
            }

        }



    public Dimension getPreferredSize() {
        return new Dimension(250, 200);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawString("This is my custom Panel!", 10, 20);
        g.setColor(Color.RED);
        g.fillRect(squareX, squareY, squareW, squareH);
        g.setColor(Color.BLACK);
        g.drawRect(squareX, squareY, squareW, squareH);
    }

}
4

1 回答 1

4

我正在考虑将 posX 变成一个数组并为其添加一个 for 循环,

不,您不需要循环。

moveSquareRight(posX);

不需要将 posX 作为参数传递,因为它是一个类变量,每个方法都可以访问它。

尽管 posX 没有改变,因此它保持在同一个位置。

所以更新posX。

if (squareX == x) {
    repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
    squareX = x + 1;
    repaint(squareX, squareY, squareW + OFFSET, squareH + OFFSET);
}

你让上面的代码太复杂了。尝试类似:

posX++;
repaint();

您不想将矩形指定为 repaint(),因为在矩形被绘制到新位置之前,旧的绘制不会被清除。

您还应该查看Motion Using the Keyboard以了解移动组件的方法。

于 2013-06-11T00:08:32.307 回答