当前位置: 代码迷 >> java >> 使矩形在面板上移动
  详细解决方案

使矩形在面板上移动

热度:54   发布时间:2023-07-31 12:00:13.0

我正在创建蛇游戏,我想从最基本的角度开始,即在屏幕上移动三角形。 但是我在如何使矩形在屏幕上移动方面遇到麻烦

这是我到目前为止的代码:

Screen.java

public class Screen extends JPanel implements ActionListener, KeyListener {
    public static final JLabel statusbar = new JLabel("Default");
    public static final int WIDTH = 800, HEIGHT = 800;
    Timer t = new Timer(50, this);
    int x = 400;
    int y = 400;
    int velx = 0;
    int vely = 0;

    private BodyPart b;

    public Screen(){

        b = new BodyPart(x, y);
        t.start();
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
    }

    public static void main(String[] args) {

    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(new Color(10, 50, 0));
        g.fillRect(0, 0, WIDTH, HEIGHT);

        g.setColor(Color.BLACK);
        for(int i = 0; i < WIDTH / 10; i++) {
            g.drawLine(i * 10, 0, i * 10, HEIGHT);
        }

        for(int i = 0; i < HEIGHT / 10; i++) {
            g.drawLine(0, i * 10, WIDTH, i * 10);
        }

        b.draw(g);
    }

    public void up(){
        vely = -10;
        velx = 0;
    }
    public void down(){
        vely = 10;
        velx = 0;
    }
    public void left(){
        vely = 0;
        velx = -10;
    }
    public void right(){
        vely = 0;
        velx = 10;
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        repaint();
        x += velx;
        y += vely;
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        switch(key){
        case KeyEvent.VK_DOWN: down();
            System.out.println("Down");
            break;
        case KeyEvent.VK_UP: up();
            System.out.println("up");
            break;
        case KeyEvent.VK_LEFT: left();
            System.out.println("left");
            break;
        case KeyEvent.VK_RIGHT: right();
            System.out.println("right");
            break;
        }        
    }

    @Override
    public void keyReleased(KeyEvent e) {}
}

BodyPart.java

public class BodyPart {

    int x;
    int y;

    public BodyPart(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void draw(Graphics g) {
    g.setColor(Color.red);
        g.fillRect(x, y, 10, 10);
        g.setColor(Color.white);
        g.drawRect(x, y, 10, 10);
    }
}

框架

public class Frame extends JPanel {
    private static JLabel statusbar = new JLabel("Default");

    public Frame(){
        statusbar = Screen.statusbar;
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        Screen s = new Screen();
        BodyPart b = new BodyPart(400,400);
        f.add(s);
        f.add(statusbar, BorderLayout.SOUTH);
        f.setSize(800, 800);
        f.setVisible(true);
        f.setLocationRelativeTo(null);
        f.setResizable(false);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

因此,当我运行代码时,矩形仅显示在设置它的中心,并且不会移动。 反正有解决办法吗? 此外,状态栏也不会移动。 (有些奇怪,当我将所有内容放在一个文件中时,它确实起作用了)

另外,我计划使用linkedlist<Point> ,是否可以将此程序与linkedlist<>混合使用? 还是我必须更改代码才能使用linkedlist<>

谢谢

我对Java不太熟悉,但似乎您从未调用BodyPart.draw()方法来更新BodyPart 每当您要更新BodyPart b的位置时,请调用b.draw(g)并传递Graphics g对象。

对于状态栏,我认为这将帮助您了解main(String[] args)方法仅在main / starting类中运行。 它旨在为您的程序提供一个起点。 因此,您应该在Frame类中重命名该方法并调用它,否则您可以将代码移至Frame类的构造函数中,并在初始化Frame时运行

我不明白为什么您不应该使用LinkedList<>

您的问题如下:

从那时起,您只用x = 400和y = 400创建了BodyPart对象,则只更改了Screen类中的变量x和y的值,这是错误的,因为Screen.x与BodyPart.x不同,所以当您这样做时如Screen.x + = 10之类的东西,您还需要从BodyPart更新x变量。

一个快速解决问题的方法可能是这样的:

在您的draw方法中添加2个参数,并传递图形对象,x和y更新值。

@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(new Color(10, 50, 0));
    g.fillRect(0, 0, WIDTH, HEIGHT);

    g.setColor(Color.BLACK);
    for(int i = 0; i < WIDTH / 10; i++) {
        g.drawLine(i * 10, 0, i * 10, HEIGHT);
    }

    for(int i = 0; i < HEIGHT / 10; i++) {
        g.drawLine(0, i * 10, WIDTH, i * 10);
    }

    b.draw(g, x, y);
}

接收x和y更新的值,并更新BodyPart x和y变量。

  public void draw(Graphics g, int x, int y) {
    this.x = x;
    this.y = y;

    g.setColor(Color.red);
    g.fillRect(x, y, 10, 10);
    g.setColor(Color.white);
    g.drawRect(x, y, 10, 10);
}
  相关解决方案