0
每次嘗試引用在pain方法中創建的Drawer類時,它都會給出空指針異常。但是,除了繪畫功能之外,這個類還是完美的。我製作的遊戲類也在paint方法中工作。我不確定爲什麼我不能參考抽屜類。我無法引用繪圖中的類(圖形g)
public class Main extends Applet implements Runnable, KeyListener {
public static boolean CONSOLE = true;
// Integers
public final String Title = "";
public final int res_X = 800;
public final int res_Y = 600;
private Image image;
private Graphics second;
private URL base;
public Game game;
public Drawer drawer;
public void init() {
setSize(res_X, res_Y);
setBackground(Color.GRAY);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle(Title);
try {
base = getDocumentBase();
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
**Drawer drawer = new Drawer();**
Thread mainThread = new Thread(this);
mainThread.start();
Thread gameThread = new Thread(game = new Game(drawer));
gameThread.start();
}
public void run() {
while (true)
repaint();
}
public void paint(Graphics g) {
**System.out.println((drawer == null)?"Null":"Exists");**
^if I replace with game it returns Exists
g.fillRect(0, 0, res_X, 20);
g.fillRect(0, res_Y - 20, res_X, 20);
g.fillRect(0, 0, 20, res_Y);
g.fillRect(res_X - 20, 0, 20, res_Y);
g.fillRect(game.player.xPos, game.player.yPos, 20, 60);
}