一、创建JFrame窗口
public class TankFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static int Frame_Width = 500;
public static int Frame_Height = 500;
public void launchFrame() {
//窗口大小及位置
setSize(Frame_Width, Frame_Height);
setLocation(1000, 100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new TankFrame().launchFrame();
}
二、定义坦克类
set get函数eclipse可以自动生成,百度可查方法
1.Tank是个抽象父类,因为坦克会有敌我两种
public abstract class Tank {
// 位置
int local_x;
int local_y;
// 大小
int width = 20;
int height = 20;
// 速度
int speed=5;
// 运动方向
Direction dir;
// 炮筒方向
Direction pdir;
// 坦克图片
Image tankImage;
//敌我
boolean is_enemy;
//死亡
boolean is_Dead;
public int getLocal_x() {
return local_x;
}
public void setLocal_x(int local_x) {
this.local_x = local_x;
}
public int getLocal_y() {
return local_y;
}
public void setLocal_y(int local_y) {
this.local_y = local_y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public Direction getPdir() {
return pdir;
}
public void setPdir(Direction pdir) {
this.pdir = pdir;
}
public Image getTankImage() {
return tankImage;
}
public void setTankImage(Image tankImage) {
this.tankImage = tankImage;
}
public boolean isIs_enemy() {
return is_enemy;
}
public void setIs_enemy(boolean is_enemy) {
this.is_enemy = is_enemy;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public Tank(int local_x, int local_y, boolean is_enemy) {
super();
this.local_x = local_x;
this.local_y = local_y;
this.is_enemy = is_enemy;
}
public abstract void draw();//绘制相关函数(获取图片)
public abstract void move();//移动函数
public abstract Rectangle getRec();//获取当前位置,返回对应矩形
public abstract Rectangle getNextRec();//获取移动后的位置,返回对应矩形
}
Direction是自定义的一个枚举类型
public enum Direction {
U,D,L,R,Stop
}
2.创建MyTank类,继承Tank父类
public class MyTank extends Tank {
public MyTank(int local_x, int local_y, boolean is_enemy) {
super(local_x, local_y, is_enemy);
// TODO Auto-generated constructor stub
}
@Override
public void draw() {
}
@Override
public void move() {
}
@Override
public Rectangle getRec() {
return null;
}
@Override
public Rectangle getNextRec() {
return null;
}
}
然后重写draw()函数,获取图片,图片放在工程根目录下,可以自行百度资源,本文结束处也会提供
@Override
public void draw() {
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankU.gif"));
}
3.创建TankPanel类,所有的绘制在这个类中进行,可以避免闪烁
public class TankPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
}
重写paintComponent函数,new一个MyTank,实现绘制坦克
public class TankPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
MyTank mtank = new MyTank(100, 100, false);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, TankFrame.Frame_Width, TankFrame.Frame_Height);//背景涂黑
//绘制我方坦克
mtank.draw();
g.drawImage(mtank.getTankImage(), mtank.getLocal_x(), mtank.getLocal_y(), mtank.getWidth(), mtank.getHeight(),
this);
}
}
最后记得在TankFrame的launchFrame()中添加add(TP)
public class TankFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static int Frame_Width = 500;
public static int Frame_Height = 500;
TankPanel TP = new TankPanel();
public void launchFrame() {
//窗口大小及位置
setSize(Frame_Width, Frame_Height);
setLocation(1000, 100);
add(TP);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new TankFrame().launchFrame();
}
}
效果如下
完整源码:
TankFrame类:
public class TankFrame extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
public static int Frame_Width = 500;
public static int Frame_Height = 500;
TankPanel TP = new TankPanel();
public void launchFrame() {
//窗口大小及位置
setSize(Frame_Width, Frame_Height);
setLocation(1000, 100);
add(TP);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new TankFrame().launchFrame();
}
}
TankPanel类:
public class TankPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
MyTank mtank = new MyTank(100, 100, false);
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.fillRect(0, 0, TankFrame.Frame_Width, TankFrame.Frame_Height);
//绘制我方坦克
mtank.draw();
g.drawImage(mtank.getTankImage(), mtank.getLocal_x(), mtank.getLocal_y(), mtank.getWidth(), mtank.getHeight(),
this);
}
}
Tank类:
public abstract class Tank {
// 位置
int local_x;
int local_y;
// 大小
int width = 20;
int height = 20;
// 速度
int speed=5;
// 运动方向
Direction dir;
// 炮筒方向
Direction pdir;
// 坦克图片
Image tankImage;
//敌我
boolean is_enemy;
//死亡
boolean is_Dead;
public int getLocal_x() {
return local_x;
}
public void setLocal_x(int local_x) {
this.local_x = local_x;
}
public int getLocal_y() {
return local_y;
}
public void setLocal_y(int local_y) {
this.local_y = local_y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
public Direction getPdir() {
return pdir;
}
public void setPdir(Direction pdir) {
this.pdir = pdir;
}
public Image getTankImage() {
return tankImage;
}
public void setTankImage(Image tankImage) {
this.tankImage = tankImage;
}
public boolean isIs_enemy() {
return is_enemy;
}
public void setIs_enemy(boolean is_enemy) {
this.is_enemy = is_enemy;
}
public boolean isIs_Dead() {
return is_Dead;
}
public void setIs_Dead(boolean is_Dead) {
this.is_Dead = is_Dead;
}
public Tank(int local_x, int local_y, boolean is_enemy) {
super();
this.local_x = local_x;
this.local_y = local_y;
this.is_enemy = is_enemy;
}
public abstract void draw();//绘制相关函数(获取图片)
public abstract void move();//移动函数
public abstract Rectangle getRec();//获取当前位置,返回对应矩形
public abstract Rectangle getNextRec();//获取移动后的位置,返回对应矩形
}
MyTank类:
public class MyTank extends Tank{
public MyTank(int local_x, int local_y, boolean is_enemy) {
super(local_x, local_y, is_enemy);
// TODO Auto-generated constructor stub
}
@Override
public void draw() {
setTankImage(Toolkit.getDefaultToolkit().getImage("p2tankU.gif"));
}
@Override
public void move() {
}
@Override
public Rectangle getRec() {
return null;
}
@Override
public Rectangle getNextRec() {
return null;
}
}
enum类:
public enum Direction {
U,D,L,R,Stop
}