Java 阻止robot.mouseMove生成MouseEvent?

Java 阻止robot.mouseMove生成MouseEvent?,java,awt,mouseevent,mousemove,awtrobot,Java,Awt,Mouseevent,Mousemove,Awtrobot,我有一个3D游戏,每次我移动光标时,我都希望它重置到中间。问题是robot.mouseMove()调用MouseEvent(它确实有意义)并重置位置,所以我无法旋转 谢谢大家! 我更喜欢下面这样的代码: component.removeMouseListener(...); Robot.doSomething(); component.addMouseListener(...); 而不是设置一个标志。使用这种方法,管理侦听器的代码位于代码中的一个位置 如果使用标志,则需要 定义标志变量 设置/

我有一个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;
    }
    
    下面的基本示例检测鼠标移动到中心的距离,并更新一个简单的
    位置
    变量(基本上用作指南针点)。这有助于说明运动,但更重要的是,我们正在打破活动周期

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

    import java.awt.AWTException;
    导入java.awt.Dimension;
    导入java.awt.EventQueue;
    导入java.awt.FontMetrics;
    导入java.awt.Graphics;
    导入java.awt.Graphics2D;
    导入java.awt.Robot;
    导入java.awt.event.MouseAdapter;
    导入java.awt.event.MouseEvent;
    导入javax.swing.JFrame;
    导入javax.swing.JPanel;
    导入javax.swing.UIManager;
    导入javax.swing.UnsupportedLookAndFeelException;
    公共类TestMouseMove{
    公共静态void main(字符串[]args){
    新的TestMouseMove();
    }
    公共TestMouseMove(){
    invokeLater(新的Runnable(){
    @凌驾
    公开募捐{
    试一试{
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
    例如printStackTrace();
    }
    JFrame=新JFrame(“测试”);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(newtestpane());
    frame.pack();
    frame.setLocationRelativeTo(空);
    frame.setVisible(true);
    }
    });
    }
    公共类TestPane扩展了JPanel{
    私人机器人机器人;
    私有int位置=0;
    公共测试窗格(){
    试一试{
    bot=新机器人();
    MouseAdapter ma=新的MouseAdapter(){
    布尔ignoreMouseMove=false;
    @凌驾
    public void mouseMoved(MouseEvent e){
    如果(!ignoreMouseMove){
    ignoreMouseMove=true;
    int x=getLocationOnScreen().x+(getWidth()/2);
    int y=getLocationOnScreen().y+(getHeight()/2);
    int distanceFromCenter=e.getPoint().x-(getWidth()/2);
    位置+=与中心的距离;
    如果(位置<0){
    位置=360-位置;
    }否则,如果(位置>360){
    位置-=360;
    }
    重新油漆();
    bot.mouseMove(x,y);
    }否则{
    ignoreMouseMove=false;
    }
    }
    @凌驾
    公共无效mouseClicked(MouseEvent e){
    系统出口(0);
    }
    };
    addMouseListener(硕士);
    addMouseMotionListener(ma);
    }捕获(AWEX){
    例如printStackTrace();;
    }
    }
    @凌驾
    公共维度getPreferredSize(){
    返回新维度(200200);
    }
    @凌驾
    受保护组件(图形g){
    超级组件(g);
    Graphics2D g2d=(Graphics2D)g.create();
    FontMetrics fm=g2d.getFontMetrics();
    int x=getWidth()/2;
    int y=getHeight()/2;
    int金额=位置;
    而(x>0){
    如果(金额=位置){
    g2d.抽绳(x,y,x,y-40);
    }否则{
    g2d.抽绳(x,y,x,y-20);
    }
    字符串文本=整数。toString(金额);
    g2d.drawString(text,x-(fm.stringWidth(text)/2),y+fm.getHeight());
    x-=20;
    金额--;
    如果(金额<0){
    金额=360+金额;
    }
    }
    金额=职位+1;
    x=(getWidth()/2)+20;
    而(x360){
    位置=360-位置;
    }
    字符串文本=整数。toString(金额);
    g2d.drawString(text,x-(fm.stringWidth(text)/2),y+fm.getHeight());
    x+=
    
    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;
    }