package TanKe.lbl;
import java.awt.*;
import javax.swing.*;
public class MytankGame extends JFrame{
Mypanel mp = null;
public static void main(String[] args) {
MytankGame mg = new MytankGame();
}
//构造方法设置画布属性,定义自己的画框,并将划款添加进JFrame
public MytankGame() {
mp = new Mypanel();
this.add(mp);
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//定义自己的画框
class Mypanel extends JPanel{
Ltank LL = null;
//构造函数实例化我的坦克
public Mypanel() {
LL = new Ltank(20,20);//坦克的初始坐标
}
//重写父类的画笔方法
public void paint(Graphics g) {
super.paint(g);
//设置地图大小
g.fillRect(0, 0, 400, 300);
//坦克显示的属性
this.drawTank(20, 20, g, 0, 0);
}
//画出自己的坦克
public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.cyan);
break;
default:
g.setColor(Color.blue);
break;
}
switch (direct) {
case 0:
g.fill3DRect(x, y, 5, 20, false);
g.fill3DRect(x + 15, y, 5, 20, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y);
break;
default:
break;
}
}
}
//定义一个坦克类
class Tank {
int x = 0;
int y = 0;
//获取坦克的x,y坐标
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 Tank( int x, int y) {
this.x = x;
this.y = y;
}
}
//我方坦克类
class Ltank extends Tank {
public Ltank(int x, int y) {
super(x,y);
}
}