我有一个 3D 游戏,每次移动光标时,我都希望它重置到中间。问题是 robot.mouseMove() 调用 MouseEvent (它确实有意义)并重置位置,所以我不能旋转。
谢谢!
我有一个 3D 游戏,每次移动光标时,我都希望它重置到中间。问题是 robot.mouseMove() 调用 MouseEvent (它确实有意义)并重置位置,所以我不能旋转。
谢谢!
我更喜欢如下代码:
component.removeMouseListener(...);
Robot.doSomething();
component.addMouseListener(...);
而不是设置标志。使用这种方法,管理侦听器的代码位于代码中的一个位置。
如果您使用标志,则需要
所以你最终在课堂上的多个地方都有代码。
编辑:
关于将机器人添加到事件队列末尾的好点。因此,我将把将 MouseListener 添加回组件的代码包装在 SwingUtilities.invokeLater() 中
因为Robot
正在生成本机事件,所以该事件将(最终)进入事件队列以供 EDT 处理。
这意味着如果您尝试做类似...
removeMouseListener(...);
Robot.mouseMove(...);
addMouseListener(...);
基本上,它不会有任何效果,因为鼠标监听器的移除和附加发生在事件处理的同一周期中,这意味着机器人引发的鼠标事件将不会被处理(或稍后会出现在队列)...
相反,您需要提出某种可以检测到的标志,然后忽略下一个传入事件......
if (!ignoreMouseMove) {
ignoreMouseMove = true;
// Do your normal processing...
robot.mouseMove(...);
} else {
ignoreMouseMove = false;
}
下面的基本示例检测鼠标移动到中心的距离并更新一个简单的position
变量(它基本上充当罗盘点)。这有助于说明运动,但更重要的是,我们正在打破事件循环......
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Robot;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestMouseMove {
public static void main(String[] args) {
new TestMouseMove();
}
public TestMouseMove() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Robot bot;
private int position = 0;
public TestPane() {
try {
bot = new Robot();
MouseAdapter ma = new MouseAdapter() {
boolean ignoreMouseMove = false;
@Override
public void mouseMoved(MouseEvent e) {
if (!ignoreMouseMove) {
ignoreMouseMove = true;
int x = getLocationOnScreen().x + (getWidth() / 2);
int y = getLocationOnScreen().y + (getHeight() / 2);
int distanceFromCenter = e.getPoint().x - (getWidth() / 2);
position += distanceFromCenter;
if (position < 0) {
position = 360 - position;
} else if (position > 360) {
position -= 360;
}
repaint();
bot.mouseMove(x, y);
} else {
ignoreMouseMove = false;
}
}
@Override
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
} catch (AWTException ex) {
ex.printStackTrace();;
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
FontMetrics fm = g2d.getFontMetrics();
int x = getWidth() / 2;
int y = getHeight() / 2;
int amount = position;
while (x > 0) {
if (amount == position) {
g2d.drawLine(x, y, x, y - 40);
} else {
g2d.drawLine(x, y, x, y - 20);
}
String text = Integer.toString(amount);
g2d.drawString(text, x - (fm.stringWidth(text) / 2), y + fm.getHeight());
x -= 20;
amount--;
if (amount < 0) {
amount = 360 + amount;
}
}
amount = position + 1;
x = (getWidth() / 2) + 20;
while (x < getWidth()) {
g2d.drawLine(x, y, x, y - 20);
if (position > 360) {
position = 360 - position;
}
String text = Integer.toString(amount);
g2d.drawString(text, x - (fm.stringWidth(text) / 2), y + fm.getHeight());
x += 20;
amount++;
}
g2d.dispose();
}
}
}
不幸的是,MadProgrammer 的回答并非在所有情况下都有效,因为:
更一致的答案是不要忽略鼠标事件,而是对其进行处理,然后减去您使用机器人移动的确切数量。这是简体:
public void mouseMoved(MouseEvent me) {
mouseDiffX += me.getX() - mouseX;
mouseDiffY += me.getY() - mouseY;
mouseX = me.getX();
mouseY = me.getY();
absoluteX = me.getLocationOnScreen().x;
absoluteY = me.getLocationOnScreen().y;
}
public void moveRobot() {
int newX = canvas.getWidth() / 2 + canvas.getLocationOnScreen().x;
int newY = canvas.getHeight() / 2 + canvas.getLocationOnScreen().y;
robotMoveX = newX - absoluteX;
robotMoveY = newY - absoluteY;
robotMoved = true;
}
public void update() {
if (robotMoved) {
mouseDiffX -= robotMoveX;
mouseDiffY -= robotMoveY;
robotMoved = false;
}
finalX += mouseDiffX;
finalY += mouseDiffY;
}