title: Animation for a bouncing ball in Java?
body: <p>I'm making a JApplet for a bouncing ball in Java. </p>
  
  <p>This is what I have so far. I can make the ball move right, but I cannot make it move left again. I am also still confused on why a timer is used instead of something like an infinite loop.</p>
  
  <pre><code>import java.applet.*;
  import java.awt.*;
  import java.awt.event.*;
  
  import javax.swing.*;
  
  public class animation extends Applet implements ActionListener {
  
      Timer clock;
      boolean sleepy;
      int x = 100;
      int y = 100;
      // Initializes this applet
      public void init()
      {
          clock = new Timer(100,this);
          // fires every second
          clock.start();
      }
  
      // Called automatically after a repaint request
      public void paint(Graphics g) {
         // super.paint(g);
          g.setColor(Color.blue);
          g.fillOval(x, y, 100, 100);
          //Redraws the circles
          if(x &lt; 240){
              x = x+10;
              System.out.println(x);
          }
          if(x &gt; 240){
              x = x - 10;
              System.out.println(x);
          }
      }
  
      // Called automatically when the timer fires
      public void actionPerformed (ActionEvent e) {
          repaint();
      }
  }
  </code></pre>
  
  <p>The logic for this escapes now and I really can't think of how to make the ball go back, and keep going back until x = 0 again.</p>
  
  <p>Is there a way to implement the timer so that the ball will travel right for 5 seconds then left for 5 seconds?</p>