阻止robot.mouseMove生成MouseEvent? [英] Prevent robot.mouseMove to generate a MouseEvent?

查看:115
本文介绍了阻止robot.mouseMove生成MouseEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D游戏,每次移动光标时,我都希望它重设到中间.问题是robot.mouseMove()调用MouseEvent(确实有意义)并重置位置,因此我无法旋转.

I have a 3D game, and every time I move the cursor, I want it to reset to the middle. Problem is that robot.mouseMove() calls MouseEvent (It does make sense) and resets the position and so I can't rotate.

谢谢!

推荐答案

由于Robot正在生成本机事件,因此该事件(最终)将进入事件队列以供EDT处理.

Because Robot is generating a native event, the event will (eventually) make it's way to the Event Queue for processing by the EDT.

这意味着如果您尝试做类似的事情...

This means if you try and do something like...

removeMouseListener(...);
Robot.mouseMove(...);
addMouseListener(...);

基本上不会产生任何效果,因为在事件处理的同一周期中发生了鼠标侦听器的移除和添加,这意味着将不会处理机器人引发的鼠标事件(或将出现队列中的稍后)...

It will, basically, have no effect, as the removal and additional of the mouse listener have occurred in the same cycle of the event processing, meaning the mouse event that the robot has raised will not have being processed (or will appear later in the queue)...

相反,您需要引发一种可以检测到的标志,然后忽略下一个传入事件...

Instead, you need to raise some kind of flag that you can detect and then ignore the next incoming event...

if (!ignoreMouseMove) {
    ignoreMouseMove = true;
    // Do your normal processing...
    robot.mouseMove(...);
} else {
    ignoreMouseMove = false;
}

下面的基本示例检测到鼠标移动的中心点的距离,并更新一个简单的position变量(基本上用作指南针点).这有助于说明运动,但更重要的是,我们正在打破事件周期...

The basic example below detects the distance from the center a mouse has moved and updates a simple position variable (which basically acts as a compass point). This helps illustrate the movement, but more importantly, that we are breaking the event cycle...

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

这篇关于阻止robot.mouseMove生成MouseEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆