目录结构
四
- 给类中成员添加访问控制修饰符
- 给对象添加图片属性----Images图片工具类
编程
- 小敌机
/** 小敌机:是飞行物 */
public class Airplane extends FlyingObject {
private int speed; //移动速度
/** 构造方法 */
public Airplane(){
super(48,50);
speed = 2;
}
/** 重写step()移动 */
public void step() {
System.out.println("小敌机的y坐标向下移动了:"+speed);
}
}
- 小蜜蜂
/** 小蜜蜂:是飞行物 */
public class Bee extends FlyingObject{
private int xSpeed; //x坐标移动速度
private int ySpeed; //y坐标移动速度
private int awardType; //奖励类型(0或1)
/** 构造方法 */
public Bee(){
super(60,51);
xSpeed = 1;
ySpeed = 2;
Random rand = new Random();
awardType = rand.nextInt(2); //0或1
}
/** 重写step()移动 */
public void step() {
System.out.println("小蜜蜂的x坐标移动了:"+xSpeed+",y坐标移动了:"+ySpeed);
}
}
- 大飞机
/** 大敌机:是飞行物 */
public class BigAirplane extends FlyingObject{
private int speed; //移动速度
/** 构造方法 */
public BigAirplane(){
super(66,89);
speed = 2;
}
/** 重写step()移动 */
public void step() {
System.out.println("大敌机的y坐标向下移动了:"+speed);
}
}
- 子弹
/** 子弹:是飞行物 */
public class Bullet extends FlyingObject{
private int speed; //移动速度
/** 构造方法 */
public Bullet(int x,int y){
super(8,20,x,y);
speed = 3;
}
/** 重写step()移动 */
public void step() {
System.out.println("子弹的y坐标向上移动了:"+speed);
}
}
- 英雄机
/** 英雄机 */
public class Hero extends FlyingObject {
private int life; //命数
private int doubleFire; //火力值
/** 构造方法 */
public Hero(){
super(97,139,140,400);
life = 3;
doubleFire = 0;
}
/** 英雄机随着鼠标移动 x/y:鼠标的x坐标/y坐标 */
public void moveTo(int x,int y) {
System.out.println("英雄机随着鼠标移动啦!");
}
/** 重写step()移动 */
public void step() {
System.out.println("英雄机切换图片啦!");
}
}
- 天空
/** 天空:是飞行物 */
public class Sky extends FlyingObject {
private int speed; //移动速度
private int y1; //第2张图的y坐标
/** 构造方法 */
public Sky(){
super(400,700,0,0);
speed = 1;
y1 = -700;
}
/** 重写step()移动 */
public void step() {
System.out.println("天空的y坐标和y1坐标向下移动了:"+speed);
}
}
- 飞行物父类
/** 飞行物 */
public class FlyingObject {
protected int width; //宽
protected int height; //高
protected int x; //x坐标
protected int y; //y坐标
/** 专门给小敌机、大敌机、小蜜蜂提供的 */
public FlyingObject(int width,int height){
this.width = width;
this.height = height;
Random rand = new Random();
x = rand.nextInt(400-this.width); //x:0到(400-敌人宽)之间的随机数
y = -this.height; //y:负的敌人的高
}
/** 专门给英雄机、天空、子弹提供的 */
public FlyingObject(int width,int height,int x,int y){
this.width = width;
this.height = height;
this.x = x;
this.y = y;
}
/** 飞行物移动 */
public void step() {
System.out.println("飞行物移动啦!");
}
}
- 图片工具类
/** 图片工具类 */
public class Images {
public static BufferedImage sky; //天空
public static BufferedImage bullet; //子弹
public static BufferedImage[] heros; //英雄机数组
public static BufferedImage[] airplanes; //小敌机数组
public static BufferedImage[] bigairplanes; //大敌机数组
public static BufferedImage[] bees; //小蜜蜂数组
static {
//初始化静态图片
sky = readImage("background.png");
bullet = readImage("bullet.png");
heros = new BufferedImage[2];
heros[0] = readImage("hero0.png");
heros[1] = readImage("hero1.png");
airplanes = new BufferedImage[5];
bigairplanes = new BufferedImage[5];
bees = new BufferedImage[5];
airplanes[0] = readImage("airplane0.png");
bigairplanes[0] = readImage("bigairplane0.png");
bees[0] = readImage("bee0.png");
for(int i=1;i<airplanes.length;i++) {
airplanes[i] = readImage("bom"+i+".png");
bigairplanes[i] = readImage("bom"+i+".png");
bees[i] = readImage("bom"+i+".png");
}
}
/** 读取图片 */
public static BufferedImage readImage(String fileName) {
try{
BufferedImage img = ImageIO.read(FlyingObject.class.getResource(fileName)); //同包下读取图片
return img;
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
}
- 窗口
/** 整个窗口 */
public class World extends JPanel {
private Sky sky = new Sky(); //天空
private Hero hero = new Hero(); //英雄机
private FlyingObject[] enemies = {
}; //敌人(小敌机、大敌机、小蜜蜂)数组
private Bullet[] bullets = {
}; //子弹数组
public void action() {
//测试代码
enemies = new FlyingObject[5];
enemies[0] = new Airplane();
enemies[1] = new Airplane();
enemies[2] = new BigAirplane();
enemies[3] = new BigAirplane();
enemies[4] = new Bee();
for(int i=0;i<enemies.length;i++) {
//遍历所有敌人
FlyingObject f = enemies[i]; //获取每一个敌人
System.out.println(f.x+","+f.y);
f.step();
//f被子弹射击
//f和英雄机撞
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
World world = new World();
frame.add(world);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,700);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
world.action();
}
}
访问修饰符、异常处理、流操作
个人总结:通过io流读图片