gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:43:53 25 4
gpt4 key购买 nike

当我尝试在构造函数中为 Dino.java 中名为 dinoImage 的 BufferedImage 设置值时,每次都会出现空白屏幕(第二张图片),因为没有调用 repaint() ,但如果我将其设置为 null 它工作得很好,但没有这个图像(第一张图片)。
没有异常(exception),这段代码中的一切看起来都很好,当我尝试使用 Resource.java 的静态方法 getImage 设置该字段的值时出现此问题,该方法使用这行代码 ImageIO.read(new File(path)) 并且它导致 repaint() 没有被调用,我猜这条线会导致这种奇怪的行为,但我不知道如何解决它。

Working one Blank Screen

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

}

恐龙.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);

在框架可见之前,需要将 Swing 组件添加到框架中。否则面板的大小为 (0, 0) 并且没有任何可绘制的内容。

代码应该是这样的:

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

关于java - 如果我使用 ImageIO.read() 则不会调用 Repaint(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61208505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com