1 总体需求分析
1.1 类
1、主类
2、我方飞机类
3、线程类
2.2 效果图
2 实现
2.1 飞机类 MyPlane
飞机类包括以下参数:
位置x、y
速度vx、vy
图片图标myicom:设置为飞机的图片
飞机类包括以下方法:
有参构造方法:初始化x、y,保证第一次绘制飞机能够有坐标
public MyPlane(int x, int y) { this.x = x; this.y = y; }
绘制飞机方法:用参数画笔g来绘制飞机,再更新vx和vy,
public void drawPlane(Graphics g) { g.drawImage(myicon.getImage(), x, y, null); x += vx; y += vy; }
代码:
package com.java8.planewarsV1;
import javax.swing.*;
import java.awt.*;
public class MyPlane {
private int x, y;
private int vx, vy;
private ImageIcon myicon = new ImageIcon(this.getClass().getResource("PaperPlane.png"));
/**
* 我方飞机的构造函数
* @param x:绘制坐标x
* @param y:绘制坐标y
*/
public MyPlane(int x, int y) {
this.x = x;
this.y = y;
}
/**
* 绘制飞机对象的函数,每次调用,则绘制一次飞机
* @param g:画笔
*/
public void drawPlane(Graphics g) {
g.drawImage(myicon.getImage(), x, y, null);
x += vx;
y += vy;
}
/**
* setVx
* setVy
* 用于在按键后,设置其每次移动的大小,并在释放的时候清0
* @param vx
*/
public void setVx(int vx) {