BaelSnake
BaelSnake
采纳率13.5%
2015-07-16 10:26

JAVA小白向大神求助。mousemotionlistener

5

代码可能有点乱而长。我就是想问鼠标控制不了 hero.png这个图片的移动

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.jpd").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;

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

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(1);
        } 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;
/**

  • 当StartFrame的小飞机飞到最右边时,Start窗口隐藏(this.disapose()),显示新的窗口PlaneFrame
  • 创建新的面板
    *
    /
    public class PlaneFrame extends JFrame{
    /
    *

    • */ 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);
    PlanePanel pp = new PlanePanel();
    this.add(pp);
    pp.addMouseMotionListener(pp); //第一个pp代表面,,第二个pp代表监听器对象
    this.setVisible(true);
    //给面板添加监听器对象

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

    planeFrame = new PlaneFrame();
    }

    }

package start;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

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

public class PlanePanel extends JPanel implements MouseListener,MouseMotionListener{

// 定义布尔类型isLive(存活)

private boolean isLive = true;
int x;
ImageIcon icon = new ImageIcon("Image/hero.png");
public Object gImage = new ImageIcon("image/hero.png");
public Object gImage1 = new ImageIcon("image/hero_destory.png");
private int hero_x;
private int hero_y;
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(), x, 450,this);

    }




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

}

public void mouseMoved(MouseEvent e) {
    // TODO Auto-generated method stub
    int x = e.getX();
    int y = e.getY();
    //根据鼠标的坐标计算出英雄机的坐标
    hero_x = x-icon.getIconWidth()/2;
    hero_y =-icon.getIconHeight()/2;
    System.out.println("重写move");
    this.repaint();
}

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

}

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

}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • tongyi55555 JonsonJiao 6年前

    1、PlanePanel中icon的初始化中使用Image,应该是image吧;

    2、你的mouseMoved方法没有问题,是监听到了的,如果你是想根据你鼠标的位置重绘hero图片,你需要在paint方法中设置动态的坐标。比如改成这样:

    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);
    
        }
    
        public void mouseDragged(MouseEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        public void mouseMoved(MouseEvent e) {
            // TODO Auto-generated method stub
            int x = e.getX();
            int y = e.getY();
            // 根据鼠标的坐标计算出英雄机的坐标
            hero_x = x;
            hero_y = y;
    //      hero_x = x - icon.getIconWidth() / 2;
    //      hero_y = -icon.getIconHeight() / 2;
            System.out.println("重写move");
            this.repaint();
        }
    
    点赞 评论 复制链接分享