2014-01-26 107 views
0

爲什麼我在調試或運行時得到空白空白而不是黑色?我到處尋找,並嘗試了很多!請幫忙。我只是想讓我的屏幕變黑,因爲我是所有這些java編碼的初學者。我不相信任何錯誤的代碼,因爲我沒有得到任何錯誤。我正在使用eclipse。JFrame Java setColor和fillRect保持空白?

package com.techon.rain; 

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 JFrame frame; 
private Thread thread; 
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(); 
} 
} 
public void update() { 

} 

public void render() { 
    BufferStrategy bs = getBufferStrategy(); 
    if(bs == null) { 
     createBufferStrategy(3); 
     return; 
    } 
    Graphics g = bs.getDrawGraphics(); 
    g.setColor(Color.BLACK); 
    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("Rain"); 
    game.frame.add(game); 
    game.frame.pack(); 
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    game.frame.setLocationRelativeTo(null); 
    game.frame.setVisible(true); 

    game.start(); 
} 
} 

回答

1

取代

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

通過

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

由於同時(運行);它不會執行循環內的其他步驟。

+0

是的,'render'方法在句子中從來沒有被調用過這個錯誤! – slackmart

+0

謝謝! :D解決了問題! – Cseal69