//这个是Tank类里的一个方法
public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEIGH/2 - Bullet.HEIGH/2;
Bullet bullet = new Bullet(x, y, barrelDir, tc);
tc.bullets.add(bullet);//运行的时候出现空指针错误,说是这里,好像是没添加进去,用DeBug查的时候也是在这里就跳进了一个很多很长的代码里,不知道为什么会出现这种错误,求解,如果代码不详细我还会再贴,谢谢了
return bullet;
}我在TankClient类里有List<Bullet> bullets = new ArrayList<Bullet>();
....
public void paint(Graphics g) {
myTank.draw(g);
for(int i=0; i<bullets.size(); i++) {
bullets.get(i).draw(g);
}
}
public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEIGH/2 - Bullet.HEIGH/2;
Bullet bullet = new Bullet(x, y, barrelDir, tc);
tc.bullets.add(bullet);//运行的时候出现空指针错误,说是这里,好像是没添加进去,用DeBug查的时候也是在这里就跳进了一个很多很长的代码里,不知道为什么会出现这种错误,求解,如果代码不详细我还会再贴,谢谢了
return bullet;
}我在TankClient类里有List<Bullet> bullets = new ArrayList<Bullet>();
....
public void paint(Graphics g) {
myTank.draw(g);
for(int i=0; i<bullets.size(); i++) {
bullets.get(i).draw(g);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;public class MyTank { int x;
int y;
public static final int WIDTH = 30;
public static final int HEITHT = 30;
int X_SPEED = 5;
int Y_SPEED = 5; public MyTank(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
} private boolean isLeft = false;
private boolean isUp = false;
private boolean isRight = false;
private boolean isDown = false;
enum Direction {
L, LU, U, RU, R, RD, D, LD, STOP
};
public TankClient tc = null;
public Direction dir = Direction.L;
Direction barrelDir = dir; public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.fillOval(this.x, this.y, WIDTH, HEITHT);
g.setColor(c);
barrelDraw(g);
move();
}
public void barrelDraw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
switch (barrelDir) {
case L:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + WIDTH/2);
break;
case LU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y);
break;
case U:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y);
break;
case RU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y);
break;
case R:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT/2);
break;
case RD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT);
break;
case D:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y + HEITHT);
break;
case LD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + HEITHT);
break;
case STOP:
break;
}
} public void move() {
switch (dir) {
case L:
x = x - X_SPEED;
break;
case LU:
x = x - X_SPEED;
y = y - Y_SPEED;
break;
case U:
y = y - Y_SPEED;
break;
case RU:
x = x + X_SPEED;
y = y - Y_SPEED;
break;
case R:
x = x + X_SPEED;
break;
case RD:
x = x + X_SPEED;
y = y + Y_SPEED;
break;
case D:
y = y + Y_SPEED;
break;
case LD:
x = x - X_SPEED;
y = y + Y_SPEED;
break;
case STOP:
break;
}
if(this.dir != Direction.STOP) {
this.barrelDir = this.dir;
}
if(isLeft == true && isUp == false && isRight == false && isDown == false) dir = Direction.L;
if(isLeft == false && isUp == true && isRight == false && isDown == false) dir = Direction.U;
if(isLeft == false && isUp == false && isRight == true && isDown == false) dir = Direction.R;
if(isLeft == false && isUp == false && isRight == false && isDown == true) dir = Direction.D;
if(isLeft == true && isUp == true && isRight == false && isDown == false) dir = Direction.LU;
if(isLeft == false && isUp == true && isRight == true && isDown == false) dir = Direction.RU;
if(isLeft == false && isUp == false && isRight == true && isDown == true) dir = Direction.RD;
if(isLeft == true && isUp == false && isRight == false && isDown == true) dir = Direction.LD;
if(isLeft == false && isUp == false && isRight == false && isDown == false) dir = Direction.STOP;
} public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEITHT/2 - Bullet.HEIGHT/2;
Bullet bullet = new Bullet(x, y, barrelDir);
tc.bullets.add(bullet);
return bullet;
}
public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_RIGHT:
isRight = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
break;
}
} public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_RIGHT:
isRight = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
break;
case KeyEvent.VK_SPACE:
fire();
}
}
}
这是tank类里的全部代码
package tankwar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;public class MyTank {int x;
int y;
public static final int WIDTH = 30;
public static final int HEITHT = 30;
int X_SPEED = 5;
int Y_SPEED = 5;public MyTank(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}private boolean isLeft = false;
private boolean isUp = false;
private boolean isRight = false;
private boolean isDown = false;
enum Direction {
L, LU, U, RU, R, RD, D, LD, STOP
};public TankClient tc = null; //这里写null了,请检查public Direction dir = Direction.L;
Direction barrelDir = dir;public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.green);
g.fillOval(this.x, this.y, WIDTH, HEITHT);
g.setColor(c);
barrelDraw(g);
move();
}public void barrelDraw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.BLACK);
switch (barrelDir) {
case L:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + WIDTH/2);
break;
case LU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y);
break;
case U:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y);
break;
case RU:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y);
break;
case R:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT/2);
break;
case RD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH, this.y + HEITHT);
break;
case D:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x + WIDTH/2, this.y + HEITHT);
break;
case LD:
g.drawLine(this.x + WIDTH/2, this.y + HEITHT/2, this.x, this.y + HEITHT);
break;
case STOP:
break;
}
}public void move() {
switch (dir) {
case L:
x = x - X_SPEED;
break;
case LU:
x = x - X_SPEED;
y = y - Y_SPEED;
break;
case U:
y = y - Y_SPEED;
break;
case RU:
x = x + X_SPEED;
y = y - Y_SPEED;
break;
case R:
x = x + X_SPEED;
break;
case RD:
x = x + X_SPEED;
y = y + Y_SPEED;
break;
case D:
y = y + Y_SPEED;
break;
case LD:
x = x - X_SPEED;
y = y + Y_SPEED;
break;
case STOP:
break;
}if(this.dir != Direction.STOP) {
this.barrelDir = this.dir;
}if(isLeft == true && isUp == false && isRight == false && isDown == false) dir = Direction.L;
if(isLeft == false && isUp == true && isRight == false && isDown == false) dir = Direction.U;
if(isLeft == false && isUp == false && isRight == true && isDown == false) dir = Direction.R;
if(isLeft == false && isUp == false && isRight == false && isDown == true) dir = Direction.D;if(isLeft == true && isUp == true && isRight == false && isDown == false) dir = Direction.LU;
if(isLeft == false && isUp == true && isRight == true && isDown == false) dir = Direction.RU;
if(isLeft == false && isUp == false && isRight == true && isDown == true) dir = Direction.RD;
if(isLeft == true && isUp == false && isRight == false && isDown == true) dir = Direction.LD;
if(isLeft == false && isUp == false && isRight == false && isDown == false) dir = Direction.STOP;
}public Bullet fire() {
int x = this.x + MyTank.WIDTH/2 - Bullet.WIDTH/2;
int y = this.y + MyTank.HEITHT/2 - Bullet.HEIGHT/2;
Bullet bullet = new Bullet(x, y, barrelDir);
tc.bullets.add(bullet);
return bullet;
}public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = true;
break;
case KeyEvent.VK_UP:
isUp = true;
break;
case KeyEvent.VK_RIGHT:
isRight = true;
break;
case KeyEvent.VK_DOWN:
isDown = true;
break;
}}public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_LEFT:
isLeft = false;
break;
case KeyEvent.VK_UP:
isUp = false;
break;
case KeyEvent.VK_RIGHT:
isRight = false;
break;
case KeyEvent.VK_DOWN:
isDown = false;
break;
case KeyEvent.VK_SPACE:
fire();
}
}}
TankClient tc = new TankClient();