【发布时间】:2015-12-27 03:28:19
【问题描述】:
我为游戏设计了一个代码。问题是背景不会更改为我从图形颜色库中选择的任何颜色。
我需要有人用我提供的代码来解决这个问题(请不要制作全新的代码)。我知道为什么 java/eclipse 不会显示它???我错过了什么吗?此处的程序应显示背景颜色为蓝色的 GUI。相反,我变白了。
public class MainApp extends Canvas implements Runnable {
private static final long serialVersionUID = 8928635543572788908L;
private static final int WIDTH= 648, HEIGHT= WIDTH/ 12 * 9;
private Thread thread;
private boolean running= false;
public MainApp()
{
new Window(WIDTH, HEIGHT, "App", this);
}
public synchronized void start()
{
thread= new Thread(this);
thread.start();
running= true;
}
public void run()
{
long lastTime= System.nanoTime();
double amountOfTicks= 60.0;
double ns= 1000000000 / amountOfTicks;
double delta= 0;
long timer= System.currentTimeMillis();
int frames= 0;
while(running){
long now= System.nanoTime();
delta += (now- lastTime) / ns;
lastTime= now;
while(delta >= 1){
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
System.out.print("FPS: " + frames);
frames= 0;
}
}
stop();
}
public synchronized void stop()
{
try
{
thread.join();
running= false;
}catch(Exception e){e.printStackTrace();}
}
public void tick()
{
}
public void render()
{
BufferStrategy bs= this.getBufferStrategy();
if(bs== null)
{
this.createBufferStrategy(3);
return;
}
Graphics g= bs.getDrawGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.dispose();
bs.show();
}
public static void main(String[] args) {
new MainApp();
}
}
【问题讨论】:
-
什么时候打电话给
start? -
Start 在我的 Window 类中,我一开始在构造函数中调用它..
-
新窗口(参数)当我称之为我的 app.start();开始运行。
-
好的,请确保您没有收到
IllegalStateException组件未附加到本机对等方(即,在您尝试创建BufferStrategy之前窗口不可见),因为这是你的代码在我开始工作时所做的第一件事,但我只需稍作调整就能让它安全运行 -
我没有收到任何错误。我的 FPS 工作正常,只是我的背景不会显示。你是怎么让它工作的?