当我尝试在构造函数中的Dino.java中将值设置为BufferedImage称为dinoImage时,由于没有调用repaint(),每次(第二张图片)我都会得到一个空白屏幕,但是如果我将其设置为null,则可以正常工作但没有此图片(第一张图片)。
毫无例外,此代码中的一切似乎都很好,当我尝试使用Resource.java的静态方法getImage将此字段的值设置为该字段时,会出现此问题,该静态代码使用此行代码ImageIO.read(new File(path)),并且导致未调用repaint() ,我猜这条线会导致这种奇怪的行为,但我不知道如何解决。

java - 如果我使用ImageIO.read(),则不会调用Repaint()-LMLPHP
java - 如果我使用ImageIO.read(),则不会调用Repaint()-LMLPHP

Main.java

public class Main {

    public static void main(String[] args) {

        GameWindow gameWindow = new GameWindow();
        gameWindow.startGame();

    }
}


GameWindow.java

public class GameWindow extends JFrame {

    private GameScreen gameScreen;

    public GameWindow() {
        super("Runner");
        setSize(1000, 500);
        setVisible(true);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        gameScreen = new GameScreen();
        add(gameScreen);
    }

    public void startGame() {
        gameScreen.startThread();
    }

}


GameScreen.java

public class GameScreen extends JPanel implements Runnable, KeyListener {

    private Thread thread;

    public static final double GRAVITY = 0.1;
    public static final int GROUND_Y = 300;

    private Dino dino;

    public GameScreen() {
        thread = new Thread(this);
        dino = new Dino();
    }

    public void startThread() {
        thread.start();
    }

    @Override
    public void run() {
        while(true) {
            try {
                Thread.sleep(20);
                dino.updatePosition();
                repaint();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
//      g.clearRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.RED);
        g.drawLine(0, GROUND_Y, getWidth(), GROUND_Y);
        dino.draw(g);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        System.out.println("Key Pressed");
        dino.jump();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("Key Released");
    }

}


Dino.java

public class Dino {

    private double x = 100;
    private double y = 100;
    private double speedY = 0;
    private BufferedImage dinoImage;

    public Dino() {
        dinoImage = getImage("data/dino.png");
    }

    public void updatePosition() {
        if(y + speedY >= GROUND_Y - 100) {
            speedY = 0;
            y = GROUND_Y - 100;
        } else {
            speedY += GRAVITY;
            y += speedY;
        }
    }

    public void jump() {
        if(y == GROUND_Y - 100) {
            speedY = -5;
            y += speedY;
        }
    }

    public void draw(Graphics g) {
        g.setColor(Color.BLACK);
        g.drawRect((int)x, (int)y, 100, 100);
        g.drawImage(dinoImage, (int)x, (int)y, null);
    }

}


资源.java

public class Resource {

    public static BufferedImage getImage(String path) {
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return image;
    }

}

最佳答案

    setSize(1000, 500);
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    gameScreen = new GameScreen();
    add(gameScreen);


在使框架可见之前,需要将摆动组件添加到框架。否则,面板的大小为(0,0),并且没有油漆。

该代码应类似于:

    gameScreen = new GameScreen();
    add(gameScreen);
    setSize(1000, 500);
    setVisible(true);

10-02 03:00