/**
 File:      MoonBadgeApplet.java
 Author:    Angus McIntyre <angus@pobox.com>
 Date:      01.06.96
 Updated:   01.06.96
 
 Simple applet for drawing an appropriate image of the moon, according to
 its current phase. Based on the 'MoonApplet' class.

    HISTORY
    -------
    
    01.06.96    SLAM    First version implemented
    
    13.06.96	SLAM	Resolved flicker by deferring the call to
    					'drawImage()' until the image has been
    					fully downloaded. Set the background color
    					to black.
    
    TO-DO
    -----
    
    * Allow the user to specify the font to be used for drawing the
      name of the phase and the date.
      
    * Allow the user to specify where, relative to the image, the
      text should be drawn.
    
    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 MoonCalculation;

// Class:   MoonBadgeApplet

public class MoonBadgeApplet extends MoonApplet {

    // Some constants used to position the text (name of phase, and
    // date), plus a font selection. I should really make the font
    // configurable, but I can't be bothered.
    
    protected   static  final   int     text_x_offset = 5;
    protected   static  final   int     text_y_offset = 5;
    protected   static  final   int     font_size = 12;
    protected   static  final   String  font_face = "TimesRoman";

    // Instance variables
    
    protected   Font            font;
    protected   boolean         showDate;
    protected   boolean         showPhaseName;

    // Method: init
    //
    // Initialize the applet, being sure to call the superclass method.
    // Initialization simply reads a couple of options parameters (which
    // control whether any additional text is drawn) and sets up the font.
    // It also sets the background color to black, on the grounds that you
    // are, of course, going to be displaying the image on a black background.
    // This should probably be a parameter, but I can't be bothered.
    
    public void init() {
        super.init();
        showDate = parseBooleanParameter("show-date",false);
        showPhaseName = parseBooleanParameter("show-phase-name",false);
        font = new Font(font_face,Font.BOLD,font_size);
        setBackground(Color.black);
    }
    
   // Method:  getAppletInfo()
   //
   // Return information about the applet. 

   public String getAppletInfo() {
      return "MoonBadgeApplet 1.01 by Angus McIntyre <angus@pobox.com>";
   }
   
   // Method: getParameterInfo()
   //
   // Return information about the applet's parameter options. Read this
   // if you want to see what parameters this applet supports.
    
   public String[][] getParameterInfo() {
      String[][] info = {
         {"basename",			 "String",	 "base name for image files"},
         {"image-extension",	 "String",	 "file extension for images - default '.gif'"},
         {"show-phase-name",	 "Boolean",	 "print the name of the moon phase"},
         {"show-date",			 "Boolean",	 "print the date"}
      };
      return info;
   }
    
    // Method:  paint(Graphics)
    //
    // Draw the image of the moon, add text as required.
    
    public void paint(Graphics g) {
  
        // Call the calculation object (in the parent class) to find out
        // what the current phase of the moon is. Note the need to add
        // 1900 to the year, because Java returns two-digit years.
        
        int         phase = calculation.moonPhase(date.getYear() + 1900,
                                                  date.getMonth(),
                                                  date.getDate());
        int         imageHeight = images[phase].getHeight(this);
        
        // Start the process of downloading the image.
        
		this.prepareImage(images[phase],this);
						   
        // In order to get around problems with NetScape 3.0b4 - where
        // 'drawImage()' can draw an incomplete image, forcing calls to
        // 'update()' and thence to 'paint()', leading to vicious image
        // flicker - we don't do any real drawing until a call to
        // 'checkImage()' confirms that the image is fully downloaded.
        
		if ((checkImage(images[phase],this) & ALLBITS) > 0) {
		
			// Draw the image.

			g.drawImage(images[phase],0,0,null);
        
	        // If we're also displaying some text information, set up font
	        // and color. We'll need to know how tall the lines of text are
	        // so that we can position them properly. Note that I ignore
	        // leading (i.e. just use ascent and descent to calculate the
	        // height), in order to save space.
	        
	        if (showPhaseName || showDate) {
	            g.setColor(Color.white);
	            g.setFont(font);
	            FontMetrics metrics = g.getFontMetrics(font);
	            int         fontHeight = metrics.getAscent() +
	                                     metrics.getDescent();
	            
	            // Show the name of the phase if required.
	            
	            if (showPhaseName) {
	                g.drawString(calculation.phaseName(phase),
	                             	text_x_offset,imageHeight + 
	                                           	  text_y_offset);
	            }
	            
	            // And the date as well.
	            
	            if (showDate) {
	                g.drawString(date.toString(),
	                             	text_x_offset,imageHeight + 
	                                          	  fontHeight +
	                                           	  text_y_offset);
	            }
			}
		}
    }
}
