java - 如何让图像跟随鼠标移动?

标签 java image swing mousemove mouse-listeners

如何让图像跟随鼠标在屏幕上的任何位置移动?

下面的代码使图像沿 x 轴移动。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class PlayerTwo implements KeyListener, MouseListener, MouseMotionListener{
   public static int PLAYER_HEIGHT = 15;
   public static int PLAYER_WIDTH = 15;

   private Image p2Image = null;
   private static int x = 0;
   private static int y = 0;
   private int heightPosition = 0;

    Main main = null;

    public PlayerTwo(Image pi, Main m ){
        main = m;
        p2Image = pi;
        y = (int)((Main.WIDTH*2)+(PLAYER_WIDTH*2));
        heightPosition = Main.HEIGHT-PLAYER_HEIGHT-20;

    }
    public void drawPlayer(Graphics g){
        g.drawImage(p2Image, y, heightPosition, main);
    }
    public void keyTyped(KeyEvent e) {
    }
    public void keyReleased(KeyEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void mousePressed(MouseEvent e) {
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mouseDragged(MouseEvent e) {     
    }
    public void mouseMoved(MouseEvent me) {
       int newX = me.getX();
       int newY = me.getY();
       if(newY > (Main.HEIGHT+PLAYER_HEIGHT+10)){
           y = Main.HEIGHT+PLAYER_HEIGHT+10;
       }else{
           y = newY;
       }
//       if (newX > (Main.WIDTH-PLAYER_WIDTH-10)){
//           x = Main.WIDTH-PLAYER_WIDTH-10;
//       }else{
//           x = newX;
//       }
    }
}

已更新主要...

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;

public class Main extends JFrame implements Runnable {

    public static int WIDTH = 600;
    public static int HEIGHT = 600;
    private int gameSpeed = 100;
    PlayerOne playOne = null;
    PlayerTwo playTwo = null;
    Image p1Image = null;
    Image p2Image = null;
    Image backImage = null;
    Graphics offscreen_high;
    BufferedImage offscreen;

    public Main(String frameTitle) {
        super(frameTitle);

        p1Image = new javax.swing.ImageIcon("src/resources/player1.gif").getImage();
        p2Image = new javax.swing.ImageIcon("src/resources/player2.gif").getImage();
        backImage = new javax.swing.ImageIcon("src/resources/back.png").getImage();

        offscreen = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        offscreen_high = offscreen.createGraphics();

        playOne = new PlayerOne(p1Image, this);
        playTwo = new PlayerTwo(p2Image, this);

        addKeyListener(playOne);
        addKeyListener(playTwo);

        addMouseListener(playTwo);
        addMouseMotionListener(playTwo);


        setSize(WIDTH, HEIGHT);
        setVisible(true);

        startGame();
    }

    public void startGame() {
        Thread thread = new Thread(this);
        thread.start();
    }

    public void paint(Graphics g) {
        offscreen_high.setColor(Color.black);
        offscreen_high.fillRect(0, 0, WIDTH, HEIGHT);
        offscreen_high.drawImage(backImage, 0, 0, this);
        playOne.drawPlayer(offscreen_high);
        playTwo.drawPlayer(offscreen_high);
        g.drawImage(offscreen, 0, 0, this);
    }

//   public void update(Graphics g){
//       paint(g);
//   }
    public void run() {
        int count = 0;
        while (true) {
            try {
                Thread.sleep(gameSpeed);
            } catch (InterruptedException ie) {
            }
            repaint();
            count++;
        }


    }

    public static void main(String[] args) {
        Main main = new Main("Game On!");
    }

}

最佳答案

通常,您需要某种方式来告诉 UI 它应该更新。

假设 Main 是某种组件(并且它还负责绘制 Player),您应该在 mouseListener 中调用它的 repaint 方法

但是如果没有更多细节,这更多的是猜测

已更新

在对代码进行了一番修改之后,在我看来,主要问题是您尝试使用垂直位置(y)仅在水平轴(x)上绘制图像...

public void drawPlayer(Graphics g){
    //g.drawImage(p2Image, y, heightPosition, main);
    g.drawImage(p2Image, x, heightPosition, main);
}

要使其正常工作,您必须取消 mouseMoved 方法中代码的注释,以便 x 位置更新。

您还应该避免绘制到顶级容器,主要原因(除了您可能搞砸绘制过程的事实之外)是顶级容器不是双缓冲的。

相反,您应该将整个游戏容器移动到诸如 JPanel 之类的东西,并重写它的 paintComponent 方法(并且不要调用 super.paintComponent)

关于java - 如何让图像跟随鼠标移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17003206/

相关文章:

python - 在图像上插入文本[2],python

c++ - 奇怪的图像行为(Qt Quick 2)

image - 使用 cli 查找所有像素的平均值

java - 如何让 Java Swing 正确绘制?

java - Spring JPA Repository保存不插入数据库

java - Wildfly 8.2 : importing wildcard certificate, 中间证书丢失

java - 多对多 : what is the correct or best way to go in JPA?

Verdana 字体按钮中的 Java 汉字

java - 如何在JFrame中显示图像

java - Android 应用程序从日期开始?