/**
 * A demo applet that uses basic animation and a timer.
 *
 * @author TK Rogers
 * @version 9-1-09
 */
 
import java.awt.*;           // Abstract Window Toolkit (awt)--graphic elements
import java.awt.event.*; // needed for using the ActionListener (see below) 
import javax.swing.*;     // swing = package with graphics & applet elements    
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// "extends JApplet" causes the AppletDemo class to inherit all of the
// applet code inside the swing package. "implements ActionListener"
// sets the AppletDemo class up so that it can listen for events such as
// those created by a timer.
public class AppletDemo extends JApplet implements ActionListener {
      /////////////////////////////////////////////////////////////////////////////////////////////////////
    // Fields (such as repeatTimer and x) are variable available
    // within the entire class
    private Timer repeatTimer = new Timer (20, this); // creates actionEvent
                                                                               // every 20 miliseconds
    private int x = 0;
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    // The init method starts the applet. Applets don't have
    // a main method.   
    public void init ( ) {     
        repeatTimer.start ( );
    }
   ////////////////////////////////////////////////////////////////////////////////////////////////////
   // The paint method automatically runs once. Afterwards it cannot
   // be calledCalling repaint( ) will once again run the paint applet
    public void paint (Graphics g) {  
               if (x >= 256) { 
               // Resets x to its original value so that the
               // animation repeats itself.
                   x = 0;
                   g.setColor (new Color (255, 255, 255)); //RGB colors
                   // Note: in Java graphics the origin is the upper left corner
                   // of the screen. Increasing an object's y-coordinate moves
             //  it downward. Increasing its x-coordinate moves it to the left.
 
                                 //             0, 0 = x & y coordinates upper left corner
                   g.fillRect (0, 0, 550, 550);  // Creates a rectangle.
                   //                     550, 550 = x & y coord. lower right corner
               }
               g.setColor (new Color (255 - x, 255 - x, 255 - x));
               g.fillRect (0 + x, 0 + x, 180 + x, 180 + x);
               
               g.setColor (Color.red);
               g.drawString ("Sample Applet", 0 + x, 500 - x);
               
               g.setColor (Color.yellow);
               g.drawString ("created by BlueJ", 100 + x, 500 - x);
               x++; //Increments x
      }
      ////////////////////////////////////////////////////////////////////////////////////
      // The actionPerformed method listens for ActionEvents
      // created by the timer then takes appropriate action
      public void actionPerformed (ActionEvent e) {
          if (e.getSource() == repeatTimer) {
              repaint ( ); // This re-runs the paint method.
          }
      } //ends actionPerformed method
} //ends AppletDemo class