【发布时间】:2015-03-12 01:54:00
【问题描述】:
好的,所以我是编程新手,我正在关注 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();
}
}
【问题讨论】:
-
为什么会变黑?
-
确实,为什么?我在您的源代码中看到的唯一颜色是
Color.RED -
图形 g = bs.getDrawGraphics(); g.setColor(Color.RED); g.fillRect(0, 0, getWidth(), getHeight()); g.dispose(); bs.show();对不起,我改变了它
-
你在任何地方都没有绘画方法,也没有典型的游戏面板。
-
好吧,我不确定那是什么,但在我正在观看的教程中,我遵循了所有步骤,他的屏幕改变了颜色?我的不会
标签: java