简体   繁体   English

使矩形在面板上移动

[英]make a rectangle move on panel

I am creating a snake game, and I want to start at the very basic that moving a triangle on the screen. 我正在创建蛇游戏,我想从最基本的角度开始,即在屏幕上移动三角形。 But I am have trouble on how to make a rectangle move across on the screen 但是我在如何使矩形在屏幕上移动方面遇到麻烦

Here's the code I have so far: 这是我到目前为止的代码:

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

Frame.java 框架

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

So when I run the code, the rectangle only displays in the center which I set it to, and it doesn't move. 因此,当我运行代码时,矩形仅显示在设置它的中心,并且不会移动。 Is there anyway to solve it? 反正有解决办法吗? In addition, the status bar also doesn't move. 此外,状态栏也不会移动。 (There's something weird, when I put everything in a single file, it does work) (有些奇怪,当我将所有内容放在一个文件中时,它确实起作用了)

Also, I am planning to use linkedlist<Point> , is it possible to mix this program with the linkedlist<> ? 另外,我计划使用linkedlist<Point> ,是否可以将此程序与linkedlist<>混合使用? or I have to change my code in order to use the linkedlist<> ? 还是我必须更改代码才能使用linkedlist<>

Thank you 谢谢

I am not too familiar with Java but it seems like you are never calling the BodyPart.draw() method to update your BodyPart . 我对Java不太熟悉,但似乎您从未调用BodyPart.draw()方法来更新BodyPart Whenever you want to update the position of your BodyPart b , call b.draw(g) and pass the Graphics g object. 每当您要更新BodyPart b的位置时,请调用b.draw(g)并传递Graphics g对象。

For the status bar, I think it will help you to know that the main(String[] args) method only runs in the main/starting class. 对于状态栏,我认为这将帮助您了解main(String[] args)方法仅在main / starting类中运行。 It is intended to provide a starting point for your program. 它旨在为您的程序提供一个起点。 So you should rename the method in your Frame class and call it or you could move your code to the constructor of the Frame class and it will run when Frame is initialized 因此,您应该在Frame类中重命名该方法并调用它,否则您可以将代码移至Frame类的构造函数中,并在初始化Frame时运行

I don't see why you shouldn't be able to use a LinkedList<> . 我不明白为什么您不应该使用LinkedList<>

your problem is the following: 您的问题如下:

You create the BodyPart object with x = 400 and y = 400 from that moment you only change the values of the variables x and y in class Screen, which is wrong because the Screen.x is different from BodyPart.x, so when you do something like Screen.x += 10 you also need to update the x variable from BodyPart. 从那时起,您只用x = 400和y = 400创建了BodyPart对象,则只更改了Screen类中的变量x和y的值,这是错误的,因为Screen.x与BodyPart.x不同,所以当您这样做时如Screen.x + = 10之类的东西,您还需要从BodyPart更新x变量。

A quick solution for your problem could be something like this: 一个快速解决问题的方法可能是这样的:

Add 2 parameters to your draw method and pass the graphics object, the x and y updated values. 在您的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);
}

Receive the x and y updated values and update the BodyPart x and y variables. 接收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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM