
import java.awt.*;
import javax.swing.*;

/**
 * When this program is run, it opens a window that
 * displays an animation.  The window is redrawn about 40
 * times per second, fast enough for the user to perceive
 * changes from frame to frame as continuous motion.
 */
public class Animation extends JPanel {

   /*
    * Global state variables, which maintain their values between
    * calls to paintComponent.  They are given their initial values
    * in the init() subroutine, which is called once at the start
    * of the program.
    */
    
   int frameNumber;   // frame number, goes up by one in each frame.
   
   double[] starX, starY;  // arrays holding coordinates of 100 "stars".
   
   
   /**
    *  This init() method is called once when the program first starts
    *  to give initial values to the global state variables.
    */
   void init() {
       frameNumber = 0;
       starX = new double[100];
       starY = new double[100];
       for (int i = 0; i < 100; i++) {
          starX[i] = 800*Math.random();
          starY[i] = 450*Math.random();
       }
   }

    
    /**
     * This paintComponent() method draws the content of the
     * 800-by-600 drawing area that is displayed in the window.
     * This routine will be called about 40 times per second.
     */
    protected void paintComponent(Graphics g) {
    
        /* Update the global state variables. */
       
        frameNumber = frameNumber + 1; // increases by 1 in each frame.
    
        /* Fill entire window with black (erasing previous drawing). */

        g.setColor(new Color(0,0,70));
        g.fillRect(0, 0, 800, 480); // Background of top is dark blue.
        
        /* Draw the stars as small, light blue ovals. */
         
        g.setColor(new Color(200,200,250));
        for (int i = 0; i < starX.length; i++) {
            g.fillOval( (int)(starX[i] - 3.5), (int)(starY[i] - 3.5), 7, 7);
        }
        
        /* Some frame-based animation...  Draw a moon that rises and sets.  
           This is an example of "back-and-forth" motion. */
           
        int moonFrame = frameNumber % 400;  // period is 300 frames (about 10 seconds).
        if (moonFrame >= 200) {
            // This means moonFrame will go from 0 to 149, then in reverse back
            // from 150 to 0, and this cycle will repeat forever.
            moonFrame = 400 - moonFrame;
        }
        int moonHeight =  550 - moonFrame*2;
        g.setColor(new Color(255,255,200));
        g.fillOval( 400, moonHeight, 100, 100 );
        
        /* Draw dark green ground; this will sometimes cover the moon. */
        
        g.setColor(new Color(30,70,30));
        g.fillRect(0,480,800,120);
        
    } // end paintComponent()
    
    
    /*---------- NO NEED TO LOOK BELOW THIS LINE -----------------*/

    /**
     * Main method creates and shows the window. 
     */
    public static void main(String[] args) {
        JFrame window = new JFrame("Animated Landscape With Stars");
        Animation canvas = new Animation();
        canvas.init();  // Call the initialization function.
        canvas.setPreferredSize(new Dimension(800,600));
        window.setContentPane(canvas);
        window.pack();
        window.setResizable(false);
        window.setLocation(150,80);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        while (true) { // Loop will run forever -- or until user closes the window.
           try {
               Thread.sleep(25);  // Do nothing for 25 milliseconds (about 1/40 second);
               canvas.repaint();
           }
           catch (InterruptedException e) {
           }
        }
    }

    
}
