How do I get my image to follow my mouse?

百般思念 提交于 2020-01-13 06:37:10

问题


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

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

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

}

回答1:


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

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

Updated

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

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.

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)



来源:https://stackoverflow.com/questions/17003206/how-do-i-get-my-image-to-follow-my-mouse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!