在Java窗口中捕获(捕获)鼠标光标
发布时间:2020-12-15 08:32:49 所属栏目:Java 来源:网络整理
导读:我正在寻找一种方法,在鼠标进入窗口后捕获或捕获鼠标,就像鼠标被困在虚拟机窗口中一样,直到用户按下CTRL ALT DEL或以其他方式释放鼠标.我如何在 Java中实现这一目标?全屏不是一个选择. 编辑: 这是你的一些SSCCE.此代码会将鼠标捕获到窗口中.要离开你只需要
我正在寻找一种方法,在鼠标进入窗口后捕获或捕获鼠标,就像鼠标被困在虚拟机窗口中一样,直到用户按下CTRL ALT DEL或以其他方式释放鼠标.我如何在
Java中实现这一目标?全屏不是一个选择.
编辑: 这是你的一些SSCCE.此代码会将鼠标捕获到窗口中.要离开你只需要在生成的框架内直接移动到关闭按钮.如果您注意到鼠标试图离开时它会自动返回(0,0).我需要知道的是如何让它回到它退出的坐标.我尝试使用getX()和getY()来代替(0,0),但是机器人不会在那里返回鼠标(我认为响应时间会变慢).我也让机器人将鼠标移回crosshair.x和crosshair.y但是如果用户在正确的时刻点击,这个(以及其他人)仍然允许鼠标逃脱.有什么想法吗? 主类: import java.awt.AWTException; import java.awt.Color; import java.awt.Cursor; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferStrategy; import java.awt.image.MemoryImageSource; import java.awt.Point; import java.awt.Robot; import java.awt.Toolkit; import javax.swing.JFrame; public class Game extends JFrame implements MouseMotionListener,MouseListener{ private int windowWidth = 640; private int windowHeight = 480; private Crosshair crosshair; public static void main(String[] args) { new Game(); } public Game() { this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); this.setSize(windowWidth,windowHeight); this.setResizable(false); this.setLocation(0,0); this.setVisible(true); this.createBufferStrategy(2); addMouseMotionListener(this); addMouseListener(this); initGame(); while(true) { long start = System.currentTimeMillis(); gameLoop(); while(System.currentTimeMillis()-start < 5) { //empty while loop } } } private void initGame() { hideCursor(); crosshair = new Crosshair (windowWidth/2,windowHeight/2); } private void gameLoop() { //game logic drawFrame(); } private void drawFrame() { BufferStrategy bf = this.getBufferStrategy(); Graphics g = (Graphics)bf.getDrawGraphics(); try { g = bf.getDrawGraphics(); Color darkBlue = new Color(0x010040); g.setColor(darkBlue); g.fillRect(0,windowWidth,windowHeight); drawCrossHair(g); } finally { g.dispose(); } bf.show(); Toolkit.getDefaultToolkit().sync(); } private void drawCrossHair(Graphics g){ Color yellow = new Color (0xEDFF62); g.setColor(yellow); g.drawOval(crosshair.x,crosshair.y,40,40); g.fillArc(crosshair.x + 10,crosshair.y + 21,20,-45,-90); g.fillArc(crosshair.x - 1,crosshair.y + 10,-135,-90); g.fillArc(crosshair.x + 10,crosshair.y - 1,-225,-90); g.fillArc(crosshair.x + 21,-315,-90); } @Override public void mouseDragged(MouseEvent e) { //empty method } @Override public void mouseMoved(MouseEvent e) { crosshair.x = e.getX(); crosshair.y = e.getY(); } private void hideCursor() { int[] pixels = new int[16 * 16]; Image image = Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(16,16,pixels,16)); Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor(image,new Point(0,"invisiblecursor"); getContentPane().setCursor(transparentCursor); } public void mouseExited(MouseEvent e) { System.out.println("Event: " + e); try { Robot robot = new Robot(); robot.mouseMove(0,0);// When I use (getX(),getY()) instead of (0,0) the robot will not move the mouse at all even though getX() and getY() are the coordinates I want the mouse to be moved to. Also the mouse can still escape,even when crosshair.x and crosshair.y are used as the coordinates. It seems that robot is too slow. } catch (AWTException ex) { ex.printStackTrace(); } } public void mouseEntered(MouseEvent e){ } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } } 另一类: public class Crosshair{ public int x; public int y; public Crosshair(int x,int y) { this.x = x; this.y = y; } } 解决方法
我想你可以使用
Global Event Listener来监听帧的鼠标输入事件.然后在鼠标退出事件上,我想你可以使用机器人重置鼠标离开框架时的位置.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |