title: How to make an object follow a mouse (java)
body: <p>I need help with trying to make the circle in this program continuously follow my mouse's x and y coordinates.It does follow my mouse in certain points. I think this is due to the fact that i am using the slope between the two coordinates in my program. An example of the issue would be that it will go in the exact opposite direction and move away from my mouse if I am to the right of its x position. Can someone give me some guidance?</p>
  
  <p>The <strong>ObjectMove</strong> class.</p>
  
  <pre><code>import java.awt.*;
  import hsa.LASSConsole;
  
  public class ObjectMove
  {
      static LASSConsole c;           // The output console
  
      public static void main (String[] args) throws java.lang.InterruptedException
      {
          c = new LASSConsole ();
  
          double xcoord = c.maxx () / 2;
          double ycoord = c.maxy () / 2;
          double mouseX;
          double mouseY;
          double vely;
          double velx;
          double m;
          int d = 20;
  
          while (true)
          {
              mouseX = c.GetMouseX ();
              mouseY = c.GetMouseY ();
  
              m = ((mouseY - ycoord) / (mouseX - xcoord));
  
              if (((mouseX - xcoord) / -1) == (mouseX - xcoord) / (mouseX - xcoord))
              {
                  velx = -1;
              }
              else
              {
                  velx = 1;
              }
  
              if (((mouseY - ycoord) / -1) == (mouseY - ycoord) / (mouseY - ycoord) &amp;&amp; velx == -1)
              {
                  vely = -m;
              }
              else if ((((mouseY - ycoord) / -1) == (mouseY - ycoord) / (mouseY - ycoord)) &amp;&amp; velx == 1)
              {
                  vely = m;
              }
              else if ((((mouseY - ycoord) / -1) != (mouseY - ycoord) / (mouseY - ycoord)) &amp;&amp; velx == -1)
              {
                  vely = -m;
              }
              else if (mouseX - xcoord == 0)
              {
                  if (mouseY &gt; ycoord)
                  {
                      vely = 1;
                      velx = 0;
                  }
                  else
                  {
                      vely = -1;
                  }
              }
              else
              {
                  vely = m;
  
              }
              c.setColor (Color.black);
              xcoord = xcoord - velx;
              ycoord = ycoord - vely;
              c.fillOval ((int) xcoord, (int) ycoord, (int) d, (int) d);
              Thread.sleep (100);
              c.setColor (Color.white);
              c.fillOval ((int) xcoord, (int) ycoord, (int) d, (int) d);
          }
  
  
  
      } // main method
  } // ObjectMove class
  `
  </code></pre>