我正在制作一个Isometric游戏环境,我希望使用按住鼠标右键在环境中导航,然后移动鼠标以查看地图周围。

机芯第一次工作正常,但是当我第二次尝试时,它会在再次移动之前将自身重置为原始位置。我知道这与移动背景的鼠标位置有关,但是我尝试了多种解决方案,但逻辑仍然困扰着我,请问有人能帮助我弄清楚我做错了什么。

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class CameraMovementTest extends JPanel{

private Timer timer;
private int DELAY = 10;
private  CustomMouseListener mouseListener;
private int positionX = 0, positionY = 0;

public CameraMovementTest() {

    mouseListener  = new CustomMouseListener();
    this.addMouseListener(mouseListener);
    this.addMouseMotionListener(mouseListener);
    this.setSize(500,500);
    this.setVisible(true);

    //Swing Timer
    timer = new Timer(DELAY, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            update();
            repaint();
            validate();
        }
    });
    timer.start();
}

private void update() {
    if(mouseListener!=null){
        positionX = mouseListener.getX();
        positionY = mouseListener.getY();
    }
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.fillRect( positionX,  positionY, 300,300);
}

public class CustomMouseListener implements MouseListener, MouseMotionListener {
    private int positionX = 0, positionY = 0;
    private int mouseClickX, mouseClickY;

    @Override
    public void mousePressed(MouseEvent evt) {

        if(SwingUtilities.isRightMouseButton(evt)) {

            mouseClickX = evt.getX();
            mouseClickY = evt.getY();
        }
    }
    @Override
    public void mouseDragged(MouseEvent evt) {
        if(SwingUtilities.isRightMouseButton(evt)) {
            positionX = mouseClickX - evt.getX();
            positionY = mouseClickY - evt.getY();
        }
    }
    @Override
    public void mouseMoved(MouseEvent arg0) {}
    @Override
    public void mouseClicked(MouseEvent arg0) { }
    @Override
    public void mouseEntered(MouseEvent arg0) { }
    @Override
    public void mouseExited(MouseEvent arg0) {}

    @Override
    public void mouseReleased(MouseEvent arg0) {}
    public int getX(){
        return positionX;
    }
    public int getY(){
        return positionY;
    }
}


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame f = new JFrame();
            f.setVisible(true);
            f.setSize(new Dimension(500,500));
            f.setContentPane(new CameraMovementTest());
        }
    });
}


}

最佳答案

它将自己重置到原始位置,然后再次移动。


您是否添加了任何调试代码来查看x / y位置的计算?

@Override
public void mousePressed(MouseEvent evt) {

    if(SwingUtilities.isRightMouseButton(evt)) {
        mouseClickX = evt.getX();
        mouseClickY = evt.getY();
    }
}

@Override
public void mouseDragged(MouseEvent evt) {
    if(SwingUtilities.isRightMouseButton(evt)) {
        System.out.println(mouseClickX + " : " + evt.getX());
        positionX = mouseClickX - evt.getX();
        positionY = mouseClickY - evt.getY();
    }
}


您将mouseClickX / Y设置为鼠标点,然后在mouseDragged中从值中减去鼠标点。这意味着您基本上得到了0(实际上是1,因为鼠标移动了1个像素以生成拖动事件)。

因此,在mousePressed逻辑中,您不能仅将mouseClickX / Y重置为当前鼠标点。您还需要跟踪最后一个鼠标点:

@Override
public void mousePressed(MouseEvent evt) {

    if(SwingUtilities.isRightMouseButton(evt)) {
        //mouseClickX = evt.getX();
        //mouseClickY = evt.getY();
        mouseClickX = positionX + evt.getX();
        mouseClickY = positionY + evt.getY();
    }
}

09-20 20:00
查看更多