2 baelsnake BaelSnake 于 2015.07.17 14:31 提问

如何在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();

        }
    }
}

}

3个回答

CSDNXIAOD
CSDNXIAOD   2015.07.17 14:35
已采纳

小型DBMS(c实现)
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

BaelSnake
BaelSnake 是人还是机器人
2 年多之前 回复
caozhy
caozhy   Ds   Rxr 2015.07.17 14:39
BaelSnake
BaelSnake 我能知道我的代码哪里出问题了吗
2 年多之前 回复
tongyi55555
tongyi55555   2015.07.17 14:57

上一个问题帮你回答了,你能先采纳了不。

BaelSnake
BaelSnake
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片