title: How to stop an animation in Java?
body: <p>I need to know how to stop an object in Java Graphics from moving after the Thread starts. The animation is done by changing the properties of the indicated shape. I tried to put a condition on <code>run()</code> to make the object stop moving but it seems like it did not go through the condition. </p>
  
  <p>This is the code for the object I will animate. </p>
  
  <pre><code>import java.awt.*; 
  import java.awt.geom.*; 
  public class House implements DrawingObject
  {
      private double x;
      private double y; 
      private Color roofC;
      private Color baseC; 
      private Triangle roof; 
      private Square base; 
      private double speed; 
      private double size; 
      public House(double a, double b, Color r, Color ba, double sz)
      {
          x = a; 
          y = b; 
          roofC = r; 
          baseC = ba; 
          size = sz;
          roof = new Triangle(x, y, x + (size / 2), y + (size / 3), x - (size / 2), y + (size / 3), roofC); 
          base = new Square(x - (size / 2), y + (size / 3), size, baseC); 
          speed = -5; 
      }
  
      public double getX()
      {
          return x; 
      }
  
      public void stop()
      {
          speed = 0; 
      }
      //these are the methods to be overridden when implementing DrawingObject
      public void draw(Graphics2D g2d, AffineTransform at)
      {
          roof.draw(g2d, affn); 
          base.draw(g2d, affn); 
      }
  
      public void animate()
      {
          roof.incX(speed); 
          base.incX(speed);
      }
  </code></pre>
  
  <p>And this is the code where I draw the object and call its <code>animate()</code> method.</p>
  
  <pre><code>import java.awt.*; 
  import java.awt.geom.*; 
  import javax.swing.*; 
  import java.util.*; 
  import java.awt.event.*; 
  import javafx.scene.input.KeyCode; 
  
  public class DrawingComponent extends JComponent implements Runnable
  {
      private int width; 
      private int height; 
      private JFrame frame; 
      private boolean running; 
      private int delay; 
      private ArrayList&lt;DrawingObject&gt; dos = new ArrayList&lt;DrawingObject&gt;(); 
      private MyKeyListener mkl; 
      private Graphics2D g2d; 
  
      private House h; 
  
      public DrawingComponent(JFrame f)
      {
          frame = f; 
          running = false; 
          delay = 100; 
          h = new House(640, 200, Color.RED, Color.WHITE, 100); 
      }
  
      protected void paintComponent (Graphics g)
      {
          g2d = (Graphics2D) g; 
  
          AffineTransform reset = g2d.getTransform();
  
          RenderingHints rh = 
              new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  
          g2d.setRenderingHints(rh);
  
  
  
  
          //move to 250, 200
          h.draw(g2d, reset); 
          dos.add(h); 
      }
  
      public void activateKL()
      {
          mkl = new MyKeyListener(); 
          frame.getContentPane().addKeyListener(mkl); 
          frame.getContentPane().setFocusable(true); 
      }
  
      class MyKeyListener implements KeyListener 
      {
  
          Thread t1 = new Thread(DrawingComponent.this);
          public void keyTyped(KeyEvent e)
          { 
              if(!running)
              {
                  running = true; 
                  t1.start(); 
              }
          }
  
          public void keyPressed(KeyEvent e)
          { 
              if(!running)
              {
                  running = true; 
                  t1.start(); 
              }
  
          }
  
          public void keyReleased(KeyEvent e)
          {
          }
      }
  
      public void run() 
      {
          while(running)
          {
              animateAll();  
              repaint(); 
              try
              {
                  Thread.sleep(delay); 
              }
              catch (InterruptedException ex)
              {
              }
              if(h.getX() &lt;= 250)
              {
                  h.stop(); 
              }
          }
      }
  
      public void animateAll()
      {
          for (int i = 0; i &lt; dos.size(); i++)
          {
              dos.get(i).animate(); 
          }
      }
  }
  </code></pre>
  
  <p>In the <code>DrawingComponent</code> method, there is an ArrayList of <code>DrawingObject</code>s that supposedly has all the objects with that implementation. <code>animateAll()</code> calls out all the animations for each <code>DrawingObject</code> in the ArrayList.</p>
  
  <p>So I used a method here to change to stop the House object from moving, yet it still does not work. I even tried putting the conditional <code>if(h.getX() &lt;= 250)</code> inside the <code>House</code> class, and even in the <code>animateAll()</code> class. Am I missing something in my code? </p>
  
  <p>PS: It is required that I do the program like this, so suggestions such as using Timers or removing the <code>animateAll()</code> method will not be approved. </p>
  
  <p>PS 2: I'm not sure if there is an answer to this already, but I've already checked other answered but none of them has solved my problem. </p>