如何让我的图像跟随我的鼠标? [英] How do I get my image to follow my mouse?

查看:133
本文介绍了如何让我的图像跟随我的鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get my image to follow my mouse anywhere on the screen?

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

The below code makes the image move along the x axis.

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;
//       }
    }
}

更新使用Main ...

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!");
    }

}


推荐答案

通常,您需要某种方式告诉用户界面它应该更新。

Generally, you need some way to tell the UI that it should be updated.

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

Assuming that Main is some kind of component (and it's also responsible for painting the Player), you should be calling its repaint method in the mouseListener

但没有更多细节,这更像是猜测

But without more details, this is more of a guess

已更新

在查看代码后,我遇到的主要问题是您的尝试使用垂直位置(y)仅绘制水平轴(x)的图像...

After a muck around with the code, the main problem, as I see it, is your trying to draw the image only the horizontal axis (x) using the vertical position (y)...

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

要使其正常工作,您将不得不取消注释代码在你 mouseMoved 方法,以便x位置更新。

To get it to work, you're going to have to uncomment the code in you mouseMoved method so that the x position updates.

你还应该避免绘制到顶级容器,主要原因(除了你可以搞砸油漆过程的事实)是顶级容器不是双重缓冲。

You should also avoid painting to top level containers, the main reason (apart from the fact that you can screw up the paint process) is that top level containers are not double buffered.

相反,你应该移动你的整个游戏容器到 JPanel 之类的东西并覆盖它的 paintComponent 方法(并且不要调用super.paintComponent)

Instead, you should move your entire game container over to something like a JPanel and override it's paintComponent method (and don't for get to call super.paintComponent)

这篇关于如何让我的图像跟随我的鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆