/**
 File:      MoonApplet.java
 Author:    Angus McIntyre <angus@pobox.com>
 Date:      01.06.96
 Updated:   21.03.98 18:09
 
 Base class of applets for displaying information based on phases of
 the moon. Maintains a date, a set of eight images to be used (by a
 subclass) for drawing the moon) and a calculation object used to
 perform calculations. 

    HISTORY
    -------
    
    21.03.98	SLAM	Added MediaTracker and error reporting in an
    					attempt to make the thing a little more
    					manageable.
	29.11.96	SLAM	Adapted to use ExtendedApplet as a base class.
	01.06.96    SLAM    First version implemented
          
    LEGAL
    -----
    
    This software is free. It can be used and modified in any way you 
    choose, but it may not be sold, either separately or as part of a 
    collection without explicit prior permission from the author. The 
    author assumes no liability for any loss, damage or mental or 
    physical trauma you may incur through use of or inability to use 
    this software. This disclaimer must appear on any modified or 
    unmodified version of the software in which the name of the author
    also appears.
    
**/

// Imports

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.net.*;
import MoonCalculation;
import ExtendedApplet;

// Class: MoonApplet

public class MoonApplet extends ExtendedApplet {

    // number_of_phases - number of distinct phases recognised
    // by the program.
    
    protected   static  final   int     number_of_phases = 8;
    
    // default_image_extension = default extension used for image
    // files, i.e. '.gif' or '.jpg'.
    
    private     static  final   String  default_image_extension = ".gif";

    // Internal data
    
    protected   MoonCalculation     calculation;
    protected   Date                date;
    protected   Image[]             images;
    
    // Method:  init()
    //
    // Initialise instance variables and load the images.
    
    public void init() {
        calculation = new MoonCalculation();
        date = new Date();
        images = new Image[number_of_phases];
        
		// Get the images. We have to wrap this in a try block, because
		// otherwise we get into trouble with possible exceptions thrown
		// by the URL constructor.
            
        try {
        
            // Get the base name for the images, and the extension used
            // to identify image files. If none is provided, use a
            // default value. No default is provided for the base name,
            // so if the user doesn't provide one, the applet will break.
            
            String  base_image_name = this.getParameter("basename");
            String  image_extension = this.getParameter("image_extension");
            if (image_extension == null) 
                image_extension = default_image_extension;

            // Now get the images. Each image is of the form:
            //
            //  basename<n>.gif
            //
            // where '<n>' is the number of the image, from 0 to 7.
            // The images are loaded into an array, and their loading
            // is monitored by a MediaTracker which will watch to make
            // sure that they're loaded correctly.
            
            MediaTracker    tracker = new MediaTracker(this);
            for(int i = 0;i < number_of_phases; i++) {
                images[i] = this.getImage(this.getCodeBase(),
                						  base_image_name + i + image_extension);
				tracker.addImage(images[i],i);
            }
            
            // Wait for the images to come in.
            
        	try {
            	tracker.waitForAll();
       		} 
       		catch (InterruptedException e) {
				showStatus("MoonApplet: Image loading interrupted");
        	}

			// Scan the images to see if we had an error on any of them.
			// If we did, print something on the console so that the
			// user can figure out what went wrong.
			        	
        	for(int i = 0;i < number_of_phases; i++) {
        		if (tracker.isErrorID(i))
        			System.err.println("MoonApplet: Error loading " + 
        						this.getCodeBase() + 
        						base_image_name + i + image_extension);
			}
		}
        
        // Some error trapping code that produces a diagnostic which
        // may or may not be helpful.
        
        catch (RuntimeException e) {
            System.err.println("MoonApplet: Images couldn't be loaded because " 
                               + " the error " + e + " occurred"); }
    }
}

