JAVA之坦克大战(一)绘画敌我方的坦克

思路:添加Tank父类,EnemyTank和MyTank子类,Panel类(画图),主类,主类需要继承JFrame来建立框架,其中Panel和
Main都需要用构造函数,打开程序时即自启

1.Tank类:
public class Tank {
    int x;

    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 int getDir() {
        return dir;
    }

    public void setDir(int dir) {
        this.dir = dir;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    int y;
    int dir;
    int speed;
    boolean life=true;
    int type;
    Tank(int x,int y)
    {
        this.x=x;
        this.y=y;
    }
}

2.MyTank类

public class MyTank extends Tank {
    MyTank(int x,int y)
    {
        super(x,y);
    }
}

3.EnemyTank类
public class EnemyTank extends  Tank {
    EnemyTank(int x,int y)
    {
        super(x,y);
    }
}

4.MyPanel类
import java.awt.*;
import java.util.Vector;

public class MyPanel extends Panel {
    MyTank myTank =null;
    Vector enemyTanks = new Vector();//集合类
    int enemynum=12;//坦克数量
    MyPanel()
    {
        myTank =new MyTank(120,220);//初始化我的坦克的初始坐标
        for(int i=0;i

5.Main类
import javax.swing.*;

public class Main extends JFrame {
    MyPanel myPanel= new MyPanel();
    Main()
    {
        this.add(myPanel);
        this.setSize(1200,1000);
        this.setLocation(200,10);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static  void main(String agrs[])
    {
        Main main =new Main();
    }
}
JAVA之坦克大战(一)绘画敌我方的坦克_第1张图片

你可能感兴趣的:(JAVA学习)