gpt4 book ai didi

java - 为什么我的屏幕没有变红?

转载 作者:行者123 更新时间:2023-11-29 03:11:43 26 4
gpt4 key购买 nike

好的,我是编程新手,我正在按照 Youtube 上的教程来构建我自己的游戏。我的问题是我的屏幕没有变红,只是保持灰色。我确定我做错了什么,但在 eclipse 上没有错误。这是代码:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;

public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;

private Thread thread;
private JFrame frame;
private boolean running = false;

public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);

frame = new JFrame();
}

public synchronized void start() {
running = true;
thread = new Thread("Display");
thread.start();

}

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

public void run() {
while (running) {
update();
render();
}

}

public void update() {

}

public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}

public static void main(String[] args){
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("JJF");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);

game.start();


}

}

最佳答案

你实际上从来没有为 Thread 提供要运行的东西......

public synchronized void start() {
running = true;
// Not the reference to this...
thread = new Thread(this, "Display");
thread.start();

}

通过传递 this(它是实现了 RunnableGame 类的一个实例),Thread将能够调用您的 run 方法

注意:

可视区域的大小应该由组件而不是框架来定义。这可以通过覆盖 getPreferredSize 方法并返回您希望组件达到的首选可视大小来实现。否则,可视区域将是框架的大小减去它的装饰插图,这可能不符合您的期望。

在你的“游戏循环”中,你应该考虑在循环之间有一个小的延迟,以便为系统实际更新屏幕提供时间,这会减轻 Thread 的一些压力

可运行示例

Buffer

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

public class Game extends Canvas implements Runnable {

private static final long serialVersionUID = 1L;

public static int width = 300;
public static int height = width / 16 * 9;
public static int scale = 3;

private Thread thread;
private JFrame frame;
private boolean running = false;

public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);

frame = new JFrame();
}

public synchronized void start() {
running = true;
thread = new Thread(this, "Display");
thread.start();

}

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

public void run() {
while (running) {
update();
render();
// try {
// Thread.sleep(40);
// } catch (InterruptedException ex) {
// }
}

}

public void update() {

}

public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, getWidth(), getHeight());
g.dispose();
bs.show();
}

public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle("JJF");
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);

game.start();

}

}

关于java - 为什么我的屏幕没有变红?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29000635/

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