为什么不执行run方法?

时间:2021-04-23 17:35:45

I'm trying to create a square that can move by pressing keys. When I Compiled & Ran the code it wouldn't move. So I began debugging (as well as I'm capable of). The problem seems to be that the run() function isn't being called. Why is this ? My understanding was that when using the interface Runnable, the run method is called automatically. I posted all the code in action.

我正在尝试创建一个可以通过按键移动的方块。当我编译并运行代码时,它不会移动。所以我开始调试(以及我有能力)。问题似乎是没有调用run()函数。为什么是这样 ?我的理解是,当使用Runnable接口时,会自动调用run方法。我发布了所有代码。

Why isn't run() being called automatically and how can I change my program so it will call ?

为什么不自动调用run(),如何更改程序以便调用?

Game.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game extends JPanel implements Runnable{

private static final int WIDTH = 800, HEIGHT = WIDTH / 12 * 9; //Widescreen

private Thread game_thread;
private boolean running = false;
public int x_speed = 0, y_speed = 0;
public Square square;

public Game(){
    game_thread = new Thread("GameThread");
    square = new Square(this);

    addKeyListener(new KeyListener(){

        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == e.VK_A){
                x_speed = -1;
            }
            if(e.getKeyCode() == e.VK_D){
                x_speed = 1;
            }
            if(e.getKeyCode() == e.VK_S){
                y_speed = -1;
            }
            if(e.getKeyCode() == e.VK_W){
                y_speed = 1;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {

        }

        @Override
        public void keyTyped(KeyEvent e) {

        }

    });
}

public void start(){
    System.out.println("Started");
    game_thread.start();
    running = true;
    System.out.println(running);
}

public void stop(){
    try{
        running = false;
        game_thread.join();
    }catch(InterruptedException e){
        e.printStackTrace();
    }
}



public void paint(Graphics g){
    super.paint(g);

    Graphics2D g2d = (Graphics2D) g;

    g2d.setColor(Color.BLACK);
    g2d.fillRect(0, 0, WIDTH, HEIGHT);

    square.render(g2d);

}

public void update(){
    square.move();
    System.out.println(x_speed + ", " + y_speed);
}
public void run(){
    System.out.println("run method started");
    while(running){
        System.out.println("Running");
        //Update screen info
        update();

        //Re-render
        repaint();

        try{
            game_thread.sleep(10);
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}

public static void main(String args[]){
    JFrame frame = new JFrame("Moving Thangs");
    Game game = new Game();

    frame.setSize(game.WIDTH, game.HEIGHT);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(game);
    frame.setVisible(true);
    game.start();
}
}

Square.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;


public class Square {

public static final int s_WIDTH = 80, s_HEIGHT = s_WIDTH;

public int x, y;

private Game game;

public Square(Game game){
    x = 50;
    y = 50;
    this.game = game;
}

public void move(){
    if(x >= 0 && x <= game.getWidth() - s_WIDTH){
        x += game.x_speed;
    }
    if(y >= 0 && y <= game.getHeight() - s_HEIGHT){
        y += game.y_speed;
    }
}

public void render(Graphics2D g2d){
    g2d.setColor(Color.ORANGE);
    g2d.fillRect(x, y, s_WIDTH, s_HEIGHT);
}

}

1 个解决方案

#1


1  

When you create the thread using new Thread("GameThread") you don't pass this as a runnable to the thread. You need to pass it as the first argument in the constructor like new Thread(this, "GameThread") and then everything should work.

当您使用新线程(“GameThread”)创建线程时,您不会将此作为runnable传递给线程。你需要将它作为构造函数中的第一个参数传递给新的Thread(这个,“GameThread”)然后一切都应该工作。

#1


1  

When you create the thread using new Thread("GameThread") you don't pass this as a runnable to the thread. You need to pass it as the first argument in the constructor like new Thread(this, "GameThread") and then everything should work.

当您使用新线程(“GameThread”)创建线程时,您不会将此作为runnable传递给线程。你需要将它作为构造函数中的第一个参数传递给新的Thread(这个,“GameThread”)然后一切都应该工作。