当前位置: 代码迷 >> J2SE >> 在做马士兵的坦克大战,做到第十一个版本了。坦克不发炮弹,空指针错误,望指导
  详细解决方案

在做马士兵的坦克大战,做到第十一个版本了。坦克不发炮弹,空指针错误,望指导

热度:3233   发布时间:2013-02-25 00:00:00.0
在做马士兵的坦克大战,做到第十一个版本了。坦克不发炮弹,空指针异常,望指导
在做马士兵的坦克大战,做到第十一个版本了。坦克不发炮弹,空指针异常,望指导 

我看了一个钟头也没看出哪里空指针异常。各位帮我瞅瞅。 

[code=Java][/code]

TankClient类: 
import java.awt.Color; 
import java.awt.Frame; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 

class TankClient extends Frame  

public static final int GAME_WIDTH=800; 
public static final int GAME_HIGHT=600; 

Tank tank=new Tank(50, 50,this); 
Misssile misssile=null; 
Image offscreenImage =null; 



public void paint(Graphics g) { 
tank.draw(g); 
if(misssile!=null) 
misssile.draw(g); 

}  

public void update(Graphics g) { 
if(offscreenImage==null) 

offscreenImage=this.createImage(GAME_WIDTH,GAME_HIGHT); 

Graphics gofferScreen=offscreenImage.getGraphics(); 
Color c=gofferScreen.getColor(); 
gofferScreen.setColor(Color.GREEN); 
gofferScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGHT); 
paint(gofferScreen); 
g.drawImage(offscreenImage, 0, 0, null); 

TankClient () 

setBounds(10,10,GAME_WIDTH,GAME_HIGHT); 
setVisible(true); 
setTitle( "TankWar "); 
setResizable(false); 
setBackground(Color.GREEN); 
this.addKeyListener(new KeyMonitor()); 
this.addWindowListener(new WindowAdapter() { 
public void windowClosing(WindowEvent e) { 
System.exit(0); 

}); 
new Thread(new paintThread()).start(); 


public static void main(String[] args)  

TankClient frame=new TankClient(); 


private class paintThread implements Runnable 

public void run() { 
while(true) 

repaint(); 
System.out.println( "现车不停重画 "); 
try { 
Thread.sleep(10); 
} catch (InterruptedException e) { 
e.printStackTrace(); 








private class KeyMonitor extends KeyAdapter 


public void keyReleased(KeyEvent e) { 
tank.keyReleased(e); 


public void keyPressed(KeyEvent e) { 
tank.keyPressed(e); 







Tank类: 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 


public class Tank {
public static final int XSPEED=5; 
public static final int YSPEED=5; 

TankClient client=null; 
private int x,y; 

private boolean bL=false,bU=false,bR =false,bD=false; 
enum Direction {L,LU,U,RU,R,RD,D,LD,STOP}; 

private Direction dir=Direction.STOP;

public Tank(int x, int y)  

this.x = x; 
this.y = y; 

public Tank(int x, int y,TankClient tc)  

this.x = x; 
this.y = y; 
this.client=client; 

public void draw(Graphics g) 

Color c=g.getColor(); 
g.setColor(Color.RED); 
g.fillOval(x, y, 30, 30); 
  相关解决方案