course2 定义子弹类,绘制出子弹

master
Jian Hu 3 years ago
parent 680f087b25
commit 84d29842e6

Binary file not shown.

@ -0,0 +1,65 @@
package com.demo.tank.coruse2;
import java.awt.*;
public class Bullet {
private int x, y;
private Direction direction;
private static final int SPEED = 5;
private static final int WIDTH = 20;
private static final int HEIGHT = 20;
public Bullet(int x, int y, Direction direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
public void paint(Graphics g){
Color color = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(color);
move();
}
private void move() {
switch (direction){
case UP: y -= SPEED;
break;
case DOWN: y += SPEED;
break;
case LEFT: x -= SPEED;
break;
case RIGHT: x += SPEED;
break;
default:
break;
}
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
}

@ -8,6 +8,7 @@ import java.awt.event.WindowEvent;
public class TankFrame extends Frame {
Tank tank = new Tank(500, 500, Direction.UP);
Bullet bullet = new Bullet(520, 440, Direction.UP);
public TankFrame(){
setVisible(true);
@ -26,6 +27,7 @@ public class TankFrame extends Frame {
@Override
public void paint(Graphics g){
tank.paint(g);
bullet.paint(g);
}
class MyKeyListener extends KeyAdapter{

Loading…
Cancel
Save