BaelSnake 2015-07-17 06:31 采纳率: 0%
浏览 1631

如何在PlanePanel里面实现小型敌机的下落,好混乱啊

package start;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class StartFrame extends JFrame{
/**
*
*/
public int x;
public int y;
public static StartFrame startFrame;
private static final long serialVersionUID = 1L;

public StartFrame(){

//设置窗体大小
this.setSize(400, 654);
//设置窗口居中显示
//this.setLocationRelativeTo(null);
this.setLocation(150, 50);
//设置任务栏图标
this.setIconImage(new ImageIcon("image/icon.jpg").getImage());
//去掉窗体的自带边框
this.setUndecorated(true);  

StartPanel sp = new StartPanel();
this.add(sp);
this.setVisible(true);              //默认窗体可见  放在  this.add(sp);后面

//启动线程
Thread th = new Thread(sp);
th.start();

}
public static void main(String[] args) {

startFrame = new StartFrame();

}

}

package start;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import enemy.Enemy;
import enemy.EnemyMoveThread;

public class StartPanel extends JPanel implements Runnable {
/**
* 通过鼠标来控制MouseMontionListener
* 重写mouseMovie() ——————测试语句
* 给面板添加监听器
* panel.addMouseMontionListener(面板);
* 点关闭图标关闭
* 点击缩小图标
* 实现MouseListener接口
* 重写
*/
private static final long serialVersionUID = 1L;
int x;
StartPanel sp;
Enemy e;

public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);
    g.drawImage(new ImageIcon("image/startback.png").getImage(), 0, 0, this);
    g.drawImage(new ImageIcon("image/start_aircraft.png").getImage(), x,450, this);
}

public void run() {
    // TODO Auto-generated method stub
    while (true) {
        x++;
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (x > 400) {
            // 隐藏StartFrame的窗体
            StartFrame.startFrame.dispose();

            /*
             * 获得左上角的坐标 int x = StartFrame.startFrame.getLocation().x;int y
             * = StartFrame.startFrame.getLocation().y;
             */

            // 创建新的PlaneFrame窗口
            new PlaneFrame();
            break;
        }
        repaint();
    }
}

}

package start;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

import enemy.Enemy;
import enemy.EnemyMoveThread;
/**

  • 当StartFrame的小飞机飞到最右边时,Start窗口隐藏(this.disapose()),显示新的窗口PlaneFrame
  • 创建新的面板
    封装低级Enemy xy score image speed isLive panel;
    */
    public class PlaneFrame extends JFrame implements Runnable{
    /
    *

    • */ PlanePanel pp; Enemy e; public static PlaneFrame planeFrame; //定义全局变量:可以在所有位置使用 private static final long serialVersionUID = 1L;

    public PlaneFrame(){
    this.setSize(400, 654);
    //x y?

    this.setLocation(150,50);
    this.setIconImage(new ImageIcon("image/icon.jpd").getImage());
    this.setUndecorated(true);
    pp = new PlanePanel(this);
    this.add(pp);
    pp.addMouseMotionListener(pp); //第一个pp代表面,,第二个pp代表监听器对象
    //给面板添加监听器对象
    pp.addMouseListener(pp);
    EnemyMoveThread emt = new EnemyMoveThread(e);
    emt.start();
    this.setVisible(true);

    }
    public static void main(String[] args) {

    planeFrame = new PlaneFrame();
    }
    public void run() {

    //如果英雄机存活,创建敌机
    while(pp.hero_isLive){
    //x, y, speed, icon, panel
    /*
    * Random r = new Random();
    /
    ImageIcon icon = new ImageIcon("image/shoot0_0.png");
    //大类型的数据不能直接赋值给小类型的数据,需要强制性转换
    int x = (int) (Math.random()
    (pp.getWidth()-icon.getIconWidth()));
    int y = -icon.getIconHeight();
    int speed = 10;

    //创建敌机  添加集合之中
    Enemy e = new Enemy(x,y,speed,icon,pp);
    //将敌机添加到集合中
    pp.vs.add(e);
    //给敌机绑定运动的线程
    EnemyMoveThread th = new EnemyMoveThread(e);
    th.start();
    try {
        Thread.sleep(10);
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    

    }
    }

    }

package start;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import enemy.Enemy;

public class PlanePanel extends JPanel implements MouseListener,
MouseMotionListener {

// 定义布尔类型isLive(存活)
private boolean isLive = true;
int x;
int hero_x = 200;
int hero_y = 500;
boolean hero_isLive = true;
PlaneFrame f;

ImageIcon icon = new ImageIcon("Image/hero.png");
ImageIcon min = new ImageIcon("image/min.png");

// 定义一个集合存放敌机
// Vector集合避免线程异步的问题 线程安全的一个线程
public Vector<Enemy> vs = new Vector<Enemy>();

// 通 过 构造方法 传值
public PlanePanel(PlaneFrame planeFrame) {
    this.f = planeFrame;
}

public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);


    g.drawImage(new ImageIcon("image/backmain.png").getImage(), 0, 0, this);
    g.drawImage(new ImageIcon("image/min.png").getImage(), 325, 3, this);
    g.drawImage(new ImageIcon("image/close.png").getImage(), 360, 3, this);
    g.drawImage(new ImageIcon("image/hero.png").getImage(), hero_x, hero_y,
            this);
    for(int i = 0;i<vs.size();i++){
        Enemy e = vs.get(i);
        e.drawEnemy(g);
    }
}

public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub
    if (hero_isLive) {
        int x = e.getX(); // 得到鼠标的x坐标
        int y = e.getY(); // 得到鼠标的y坐标
        // 根据鼠标的坐标与英雄机的宽和高计算英雄机的坐标
        hero_x = x - icon.getIconWidth() / 2;
        hero_y = y - icon.getIconHeight() / 2;

        if (hero_x <= 400 - 55 && hero_y >= 31 && hero_y <= 650 - 90
                && hero_x >= -40) {
            this.repaint();
        }
    }
    //
}

public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    int x = e.getX();
    int y = e.getY();
    if (x > 325 && x < 325 + min.getIconWidth() && y > 3
            && y < min.getIconHeight()) {
        // 缩小窗体
        f.setState(JFrame.ICONIFIED);
    } else if (x > 360 && x < 360 + 31 && y > 3 && y < 3 + 31) {
        System.exit(0); //
    }
}

public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mousePressed1(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

package enemy;

import java.awt.Graphics;

import javax.swing.ImageIcon;

import start.PlanePanel;

public class Enemy {
int x;
int y;
int speed;
int score = 100;
ImageIcon icon;
boolean isLive = true;
PlanePanel panel;

 public Enemy(){

 }

public Enemy(int x, int y, int speed, ImageIcon icon, PlanePanel panel) {
    super();
    this.x = x;
    this.y = y;
    this.speed = speed;
    this.icon = icon;
    this.panel = panel;
}
//画敌机的方法
public void drawEnemy(Graphics g){
    if(isLive){
        g.drawImage(icon.getImage(),x,y,panel);
    }

}
public void move(){
    y += speed;
    //如果敌机飞出面板,删除该敌机,并且isLive设置成false
    if(y>panel.getHeight()){
        isLive = false;
        panel.vs.remove(this);
    }
    panel.repaint();
}

}

package enemy;

//封装敌机运动的线程类
public class EnemyMoveThread extends Thread {

// 创建成员变量e
Enemy e;

public EnemyMoveThread(Enemy e) {
    this.e = e;
}

@Override
public void run() {

    super.run();
    while (e.isLive) {
        //
        e.move();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {

            e1.printStackTrace();

        }
    }
}

}

  • 写回答

2条回答

  • threenewbee 2015-07-17 06:39
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误