title: Drawing a rectangle that moves slowly towards the point of the mouse click rather than jumping to it (JAVA)
body: <p>I am very new to JAVA and i am just a hobbyist,Just recently I have been following some tutorials on making UI and all thee looking in to learn about this, any code snippets are welcome but also more what is this function / process called so i can look deeper into different parts of swing from the <a href="https://docs.oracle.com/javase/tutorial/uiswing/TOC.html" rel="nofollow noreferrer">ORACLE DOCS</a> which is really informative, I am still working my way through the components.</p>
  
  <p>I do not have an overall goal at present and am just finding my feet, but i want to try and make a small top-down 2d maze game to learn.</p>
  
  <p><strong>One part got my attention and it was this demo at the bottom of the page</strong>
  <a href="https://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/tutorial/uiswing/painting/step3.html</a></p>
  
  <p>Basically this is the code and I am trying to make it so when I click inside the frame the rectangle moves slowly towards the point of the mouse click rather than jumping to it (and later on if possible to click in another location and it would change its course before reaching its destination.</p>
  
  <p>What kind of area do i need to be looking in to learn about this, any code snippets are welcome but also more what is this function / process called so i can look deeper into it.</p>
  
  <p>WORKING DEMO CODE BELOW </p>
  
  <pre><code>package painting;
  
  import javax.swing.SwingUtilities;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.BorderFactory;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Graphics;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseMotionListener;
  import java.awt.event.MouseMotionAdapter;
  
  public class SwingPaintDemo3 {
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() { I am Trying to make it so when I click inside the 
              public void run() {
                  createAndShowGUI(); 
              }
          });
      }
  
      private static void createAndShowGUI() {
          System.out.println("Created GUI on EDT? "+
          SwingUtilities.isEventDispatchThread());
          JFrame f = new JFrame("Swing Paint Demo");
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
          f.add(new MyPanel());
          f.pack();
          f.setVisible(true);
      } 
  }
  
  class MyPanel extends JPanel {
  
      private int squareX = 50;
      private int squareY = 50;
      private int squareW = 20;
      private int squareH = 20;
  
      public MyPanel() {
  
          setBorder(BorderFactory.createLineBorder(Color.black));
  
          addMouseListener(new MouseAdapter() {
              public void mousePressed(MouseEvent e) {
                  moveSquare(e.getX(),e.getY());
              }
          });
  
          addMouseMotionListener(new MouseAdapter() {
              public void mouseDragged(MouseEvent e) {
                  moveSquare(e.getX(),e.getY());
              }
          });
  
      }
  
      private void moveSquare(int x, int y) {
          int OFFSET = 1;
          if ((squareX!=x) || (squareY!=y)) {
              repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
              squareX=x;
              squareY=y;
              repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
          } 
      }
  
  
      public Dimension getPreferredSize() {
          return new Dimension(250,200);
      }
  
      protected void paintComponent(Graphics g) {
          super.paintComponent(g);       
          g.drawString("This is my custom Panel!",10,20);
          g.setColor(Color.RED);
          g.fillRect(squareX,squareY,squareW,squareH);
          g.setColor(Color.BLACK);
          g.drawRect(squareX,squareY,squareW,squareH);
      }  
  }
  </code></pre>
  
  <p>Thanks for any advice ;)</p>