-4
所以我正在製作塔防遊戲,當我運行代碼時,我想在那裏顯示一個網格,但我所得到的只是一個空白窗口,控制檯:線程「AWT-EventQueue-0」塔防的例外
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Room.draw(Room.java:41)
at Screen.paintComponent(Screen.java:42)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
這裏是我的代碼:
框架類:
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
public static String title = "Tower Defence";
public static Dimension size = new Dimension(700,550);
public Frame()
{
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init();
}
public void init()
{
setLayout (new GridLayout(1, 1, 0, 0));
Screen screen = new Screen();
add(screen);
setVisible(true);
}
public static void main(String args[])
{
Frame frame = new Frame();
}
}
Screen類:
import java.awt.Graphics;
import javax.swing.*;
public class Screen extends JPanel implements Runnable
{
public Thread thread = new Thread(this);
public static int myWidth, myHeight;
public static boolean isFirst = true;
public static Room room;
public Screen()
{
thread.start();
}
public void define()
{
room = new Room();
}
public void paintComponent (Graphics g)
{
if(isFirst)
{
myWidth = getWidth();
myHeight = getHeight();
define();
isFirst = false;
}
g.clearRect(0,0, getWidth(), getHeight());
room.draw(g); //Drawing the room.
}
public void run()
{
while(true)
{
if(!isFirst)
{
room.physics();
}
repaint();
try
{
Thread.sleep(1);
} catch(Exception e){}
}
}
}
室等級:
import java.awt.*;
public class Room
{
public int worldWidth = 12;
public int worldHeight = 8;
public int blockSize = 52;
public Block[][] block;
public Room()
{
}
public void define()
{
block = new Block[worldWidth][worldHeight];
for(int y=0;y<block.length;y++)
{
for(int x=0;x<block[0].length;x++)
{
block[y][x] = new Block(((Screen.myWidth/2)) - ((worldWidth*blockSize/2)) + (x * blockSize), y * blockSize, blockSize, blockSize, 0, 0);
}
}
}
public void physics()
{
}
public void draw(Graphics g)
{
for(int y=0;y<block.length;y++)
{
for(int x=0;x<block[0].length;x++)
{
block[y][x].draw(g);
}
}
}
}
塊類:
import java.awt.*;
public class Block extends Rectangle
{
public int groundID;
public int airID;
public Block(int x, int y, int width, int height, int groundID, int airID)
{
setBounds(x,y,width,height);
this.groundID = groundID;
this.airID = airID;
}
public void draw(Graphics g)
{
g.drawRect(x, y, width, height);
}
}
提前感謝!
Ungh。沒有嘗試調試的代碼轉儲。你認爲有人會調試你的代碼嗎? –
好吧,你沒有問題,你在這裏發佈你的記事本的全部內容,以及你真正期待什麼? –
我已經解決了......冷靜下來。 – Huzzah