- Timestamp:
- 09/14/18 13:29:53 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/gio/swing/SwingInputManager.java
r1130 r1138 3 3 import java.awt.AWTException; 4 4 import java.awt.Color; 5 import java.awt.Dimension; 5 6 import java.awt.MouseInfo; 6 7 import java.awt.Robot; 8 import java.awt.Toolkit; 7 9 import java.awt.event.ActionEvent; 8 10 import java.awt.event.ActionListener; … … 70 72 71 73 /** For robotically controlling the mouse position. */ 72 private Robot _robot; 74 //private Robot _robot; 75 private MouseCorrectRobot _robot; 73 76 74 77 private SwingInputManager() … … 102 105 // TODO: What to do if robot throws exception??? cts16 103 106 try { 104 _robot = new Robot();107 _robot = new MouseCorrectRobot(); 105 108 } catch (AWTException e) { 106 109 e.printStackTrace(); … … 131 134 Point screenPosition = windowToScreenPosition(position); 132 135 //System.err.println("Move to position: window adjusted; Moving to: " + screenPosition); 133 _robot.mouseMove(screenPosition.x, screenPosition.y); 136 //_robot.mouseMove(screenPosition.x, screenPosition.y); 137 final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 138 _robot.MoveMouseControlled(screenPosition.x / screenSize.getWidth(), screenPosition.y / screenSize.getHeight()); 134 139 updateCursorPosition(position.x, position.y); 135 140 } … … 551 556 552 557 } 558 559 /** 560 * Obtained from stackoverflow. https://stackoverflow.com/questions/48799393/robot-mousemove-not-moving-to-specified-location-properly 561 * Fixes a (hopefully temporary) issue with the java.awt.Robot class on some specific systems (such as David's laptop) wherein 562 * the mouseMove function would move the mouse to odd locations. 563 * @author bln4 564 * 565 */ 566 private class MouseCorrectRobot extends Robot { 567 final Dimension ScreenSize;// Primary Screen Size 568 569 public MouseCorrectRobot() throws AWTException { 570 super(); 571 ScreenSize = Toolkit.getDefaultToolkit().getScreenSize(); 572 } 573 574 private double getTav(java.awt.Point a, java.awt.Point b) { 575 return Math.sqrt((double) ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y))); 576 } 577 578 public void MoveMouseControlled(double xbe, double ybe)// Position of the cursor in [0,1] ranges. (0,0) is the upper 579 // left corner 580 { 581 582 int xbepix = (int) (ScreenSize.width * xbe); 583 int ybepix = (int) (ScreenSize.height * ybe); 584 585 int x = xbepix; 586 int y = ybepix; 587 588 java.awt.Point mert = MouseInfo.getPointerInfo().getLocation(); 589 java.awt.Point ElozoInitPont = new java.awt.Point(0, 0); 590 591 int UgyanAztMeri = 0; 592 final int UgyanAZtMeriLimit = 30; 593 594 int i = 0; 595 final int LepesLimit = 20000; 596 while ((mert.x != xbepix || mert.y != ybepix) && i < LepesLimit && UgyanAztMeri < UgyanAZtMeriLimit) { 597 ++i; 598 if (mert.x < xbepix) 599 ++x; 600 else 601 --x; 602 if (mert.y < ybepix) 603 ++y; 604 else 605 --y; 606 mouseMove(x, y); 607 608 mert = MouseInfo.getPointerInfo().getLocation(); 609 610 if (getTav(ElozoInitPont, mert) < 5) 611 ++UgyanAztMeri; 612 else { 613 UgyanAztMeri = 0; 614 ElozoInitPont.x = mert.x; 615 ElozoInitPont.y = mert.y; 616 } 617 618 } 619 } 620 621 } 553 622 }
Note:
See TracChangeset
for help on using the changeset viewer.