我正在制作等距游戏环境,我希望使用保持右键单击并移动鼠标以环顾地图环境。
第一次运动正常,但是当我第二次尝试时,它会再次移动之前将自身重置到原始位置。我知道这与鼠标位置有关,正在移动的背景,但我已经尝试了许多解决方案,但仍然逻辑躲过我,是否有人可以帮助我预测我做错了什么,提前谢谢。
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());
}
});
}
}
答案 0 :(得分:2)
在再次移动之前,它会将自身重置为原始位置。
您是否添加了任何调试代码来查看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();
}
}