1

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

下面的代码使图像沿 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!");
    }

}
4

1 回答 1

1

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

假设这Main是某种组件(并且它还负责绘制Player),您应该repaintmouseListener

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

更新

在弄乱了代码之后,主要的问题,正如我所看到的,是你试图使用垂直位置(y)仅在水平轴(x)上绘制图像......

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

为了让它工作,您将不得不取消注释您mouseMoved方法中的代码,以便 x 位置更新。

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

相反,您应该将整个游戏容器移动到类似 a 的位置JPanel并覆盖它的paintComponent方法(并且不要让 get 调用 super.paintComponent)

于 2013-06-08T21:12:49.280 回答