title: Rotating Recorded Mouse Movements [Java]
body: <p>I created a fun small project with the goal of recording natural mouse movements that I make and then play it back but orient it towards a target.</p>
  <p><strong>Question</strong>: So let's say I make and record a natural movement that ends at x: 250, y: 278, now what I want is to make the natural movement recording end at the target coordinates that I specify (EX: x: 100, y: 100).</p>
  <p>Now for the code: (Please note that the code isn't clean what so ever but It's just the first prototype if I may say so, so please have this in mind while reading it).</p>
  <p><strong>How I record movements</strong>:</p>
  <ol>
  <li>When a MOUSE_MOVED event occurs, recording starts and ends at MOUSE_PRESSED.</li>
  </ol>
  <p>(Main.recordMoves has to be set to true for the above to happen, I set it to true/false using Scanner System input.)</p>
  <ol start="2">
  <li><p>The mouseMoved method invokes the MovesRecorder.record(MouseEvent e) method whenever a MOUSE_MOVED event occurs.</p>
  </li>
  <li><p>The mousePressed method sets the Main.recordMoves boolean to false and adds the recording to a Records list.</p>
  </li>
  </ol>
  <p><strong>Mouse</strong>.java:</p>
  <pre><code>public class Mouse implements MouseListener, MouseMotionListener {
  
      private Point location = new Point(0,0);
          
      private boolean mouseOnScreen = false;
  
      private Component c;
  
      public Mouse(Component c) {
          this.c = c;
      }
  
      public void pushStraight(int x, int y) {
          if(!mouseOnScreen) {
              c.dispatchEvent(new MouseEvent(c,MouseEvent.MOUSE_ENTERED, System.currentTimeMillis(),0,x,y,
                      0,false));
              mouseOnScreen = true;
          }
          c.dispatchEvent(new MouseEvent(c,MouseEvent.MOUSE_MOVED,System.currentTimeMillis(),0,x,y,
                  0,false));
          location.setLocation(x,y);
      }
  
      @Override
      public void mouseClicked(MouseEvent e) {}
  
      @Override
      public void mousePressed(MouseEvent e) {
          if(Main.recordMoves) {
              Main.recordMoves = false;
              MovesRecorder.currentMove.time = System.currentTimeMillis() - MovesRecorder.startTime;
              MousePatterns.moves.add(MovesRecorder.currentMove);
              MovesRecorder.currentMove = null;
          }
          System.out.println(&quot;Pressed&quot;);
      }
  
      @Override
      public void mouseReleased(MouseEvent e) {}
  
      @Override
      public void mouseEntered(MouseEvent e) {
          mouseOnScreen = true;
      }
  
      @Override
      public void mouseExited(MouseEvent e) {
          mouseOnScreen = false;
      }
  
      @Override
      public void mouseDragged(MouseEvent e) {
          location.setLocation(e.getPoint());
      }
  
      @Override
      public void mouseMoved(MouseEvent e) {
          location.setLocation(e.getPoint());
          if(Main.recordMoves) MovesRecorder.record(e);
      }
  
      public Point getLocation() {
          return location;
      }
  
      public boolean isMouseOnScreen() {
          return mouseOnScreen;
      }
  
      public Component getC() {
          return c;
      }
  }
  </code></pre>
  <p><strong>MouseRecorder</strong>.java:</p>
  <pre><code>public class MovesRecorder {
  
      public static Move currentMove;
  
      public static Point lastPos; //First lastPos (startingPos) is set here by Main.java
  
      public static long startTime;
  
      public static void record(MouseEvent e) {
          if(currentMove == null) {
              currentMove = new Move(Screen.mouse);
              startTime = System.currentTimeMillis();
          }
          currentMove.movements.add(new Point(e.getPoint().x - lastPos.x,
                  e.getPoint().y - lastPos.y));
          lastPos = e.getPoint();
      }
  }
  </code></pre>
  <p><strong>Move</strong>.java:</p>
  <pre><code>public class Move {
  
      public List&lt;Point&gt; movements = new ArrayList&lt;&gt;();
  
      public long time;
  
      private Mouse m;
  
      public Move(Mouse m) {
          this.m = m;
      }
  
      public void executeEvents(int x, int y) { //This method as of now doesn't target the params.. it just does the recording
          for(Point p : movements) {
              execute(m.getLocation().x+p.x, m.getLocation().y+p.y, MouseEvent.MOUSE_MOVED);
              try {
                  Thread.sleep(time / movements.size());
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  
      private void execute(int x, int y, int event) {
          m.getC().dispatchEvent(new MouseEvent(m.getC(),event,
                  System.currentTimeMillis(),0,
                  x,y,0,false));
      }
  
  }
  </code></pre>
  <p><strong>Main</strong>.java contains a loop and a switch statement of a Scanner to take commands:</p>
  <pre><code>while(true) {
      switch (s.next()) {
          case &quot;movesRec&quot;:
              Main.recordMoves = true;
              MovesRecorder.lastPos = Screen.mouse.getLocation();
              break;
          case &quot;move&quot;:
              MousePatterns.moves.get(0).executeEvents(100,100);
              break;
      }
  }
  </code></pre>
  <p><strong>MousePatterns</strong>.java contains a single static List for moves to be added in:</p>
  <pre><code>public static List&lt;Move&gt; moves = new ArrayList&lt;&gt;();
  </code></pre>
  <p>Help will be greatly appreciated!</p>
  <p>EDIT:</p>
  <p>An example of a Recording: <a href="https://pastebin.pl/view/e461d2bd" rel="nofollow noreferrer">https://pastebin.pl/view/e461d2bd</a>
  I want it to start at X: 274 Y: 265 and end at X: 100, Y: 100</p>