Changeset 1138 for trunk


Ignore:
Timestamp:
09/14/18 13:29:53 (6 years ago)
Author:
bln4
Message:

org.expeditee.gio.swing.SwingInputManager ->

Potential fix for issue with Robots mouse move.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/gio/swing/SwingInputManager.java

    r1130 r1138  
    33import java.awt.AWTException;
    44import java.awt.Color;
     5import java.awt.Dimension;
    56import java.awt.MouseInfo;
    67import java.awt.Robot;
     8import java.awt.Toolkit;
    79import java.awt.event.ActionEvent;
    810import java.awt.event.ActionListener;
     
    7072       
    7173        /** For robotically controlling the mouse position. */
    72         private Robot _robot;
     74        //private Robot _robot;
     75        private MouseCorrectRobot _robot;
    7376       
    7477        private SwingInputManager()
     
    102105                // TODO: What to do if robot throws exception??? cts16
    103106                try {
    104                         _robot = new Robot();
     107                        _robot = new MouseCorrectRobot();
    105108                } catch (AWTException e) {
    106109                        e.printStackTrace();
     
    131134                Point screenPosition = windowToScreenPosition(position);
    132135                //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());
    134139                updateCursorPosition(position.x, position.y);
    135140        }
     
    551556               
    552557        }
     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        }
    553622}
Note: See TracChangeset for help on using the changeset viewer.