
/**
 *  A BarChar applet can display a bar chart (or "histogram") based
 *  on data supplied in applet params.  The following params are used:
 *
 *       Param Name   Type   Description
 *  ---------------- ------  ----------------------------------------
 *       BarCount     int     The number of bars in the histogram.
 *       Data1       double   The data value for the first bar.
 *       Label1      String   A label for the first bar. 
 *       Data2       double   The data value for the second bar.
 *       Label2      String   A label for the second bar. 
 *         .
 *         .
 *   BackgroundColor Color    The background color of the applet.
 *   BarColor        Color    The color of the bars.
 *   TextColor       Color    The color used for labels on the bars.
 *   BorderColor     Color    The color of a a border for the applet.
 *
 *  The height of the applet should be exactly equal to 20 + 50*BarCount.
 *  The bars will be 30 pixels high, with 20-pixel gaps between bars.
 *  The widths of the bars will be scaled to reflect the correspoinding
 *  data values, with the longest bar stretching across the applet.
 *  
 *  A Color value can be specified either as a standard Java color name
 *  (red, green, blue, black, white, etc.) or as three int values in the
 *  range 0 to 255, separated by spaces.  Illegal values are ignored.
 */

/*  Here is a sample applet tag that could be used with this applet:

      <applet code="BarChart.class" width="340" height="220">
         <param name="BarCount" value="4">
         <param name="Data1"    value="84.2">
         <param name="Label1"   value="MacOS">
         <param name="Data2"    value="91.3">
         <param name="Label2"   value="Linux">
         <param name="Data3"    value="13.7">
         <param name="Label3"   value="Windows">
         <param name="Data4"    value="43.9">
         <param name="Label4"   value="VMS">
         <param name="BackgroundColor" value="255 240 180">
         <param name="BarColor"        value="red">
         <param name="TextColor"       value="0 0 180">
         <param name="BorderColor"     value="0 0 180">
      </applet>

*/

import java.awt.*;


public class BarChart extends java.applet.Applet {


   Color borderColor;  // Color for border of the applet.
   Color textColor;    // Color to be used for text that labels the bars.
   Color barColor;     // Color to be used for the bars.

   //**** Declare array variables to hold the data for the bars.


   public void init() {
   
      // Get color data from applet params.
   
      setBackground( getColorParam("BackgroundColor", new Color(230,230,230)) );
      borderColor = getColorParam("BorderColor", Color.black);
      textColor = getColorParam("TextColor", Color.black);
      barColor = getColorParam("BarColor", new Color(180,180,255));
      
      //**** Add code to get the data for the bars from applet params.

   } // end init();


   public void paint(Graphics g) {
   
      int width = getSize().width;   // width of applet, in pixels
      int height = getSize().height; // height of applet, in pixels
      
      g.setColor(borderColor);           // draw a 2-pixel border on the applet
      g.drawRect(0,0,width-1,height-1);  
      g.drawRect(1,1,width-3,height-3);
      
      //**** Replace the following code with code to draw and label the bars. 
      
      g.setColor(barColor); // Draw a sample bar.
      g.fillRect(20,20,width-40,30);
      g.setColor(borderColor);
      g.drawRect(20,20,width-41,29);
      
      g.setColor(textColor); // Add a label to the sample bar.
      g.drawString("Your Label Here", 25, 40);
      
   } // end paint()


   //------- utility functions for getting applet parameters -------

   private int getIntParam(String paramName, int defaultValue) {
          // Get the value of the applet param with name paramName,
          // if any.  If there is no such param, return defaultValue.
          // If the value of the param represents a legal int value,
          // return that value.  If not, return defaultValue.
      String param = getParameter(paramName);
      if (param == null) // no such param
         return defaultValue;
      try {
         return Integer.parseInt(param);
      }
      catch (NumberFormatException e) {
         return defaultValue;  // param value is not a legal int
      }
   }

   private double getDoubleParam(String paramName, double defaultValue) {
          // Get the value of the applet param with name paramName,
          // if any.  If there is no such param, return defaultValue.
          // If the value of the param represents a legal double value,
          // return that value.  If not, return defaultValue.
      String param = getParameter(paramName);
      if (param == null) // no such param
         return defaultValue;
      try {
         return (new Double(param)).doubleValue();
      }
      catch (NumberFormatException e) {
         return defaultValue;  // param value is not a legal int
      }
   }

   private Color getColorParam(String paramName, Color defaultValue) {
          // Get the value of the applet param with name paramName,
          // if any.  If there is not such param, return defaultValue.
          // If the value of the param represents a Color, return
          // that color.  If not, return defaultValue.  A Color value
          // can be represented in two ways: as three integers in the
          // range 0 to 255, separated by spaces; or by one of the
          // standard Java color names, red, blue, black, gray, etc.
          // Case is not significant in color names.
      String param = getParameter(paramName);
      if (param == null) // no such param
         return defaultValue;
      param = param.trim().toLowerCase();
      if (param.length() == 0)
         return defaultValue;
      if (param.charAt(0) >= '0' && param.charAt(0) <= '9') {
            // The color must be of the form "int int int".
            // Extract the color data, and return the corresponding
            // color.  Return the default color if there is an error.
         int r=0,g=0,b=0;   // color values to be extracted from param.
         int pos = 0; // postion withint param.
         while (pos < param.length() && param.charAt(pos) >= '0' 
                                          && param.charAt(pos) <= '9') {
            r = 10*r + ((int)param.charAt(pos) - (int)'0');
            pos++;
         }
         if (r > 255)
            return defaultValue;
         while (pos < param.length() && 
                  (param.charAt(pos) == ' ' || param.charAt(pos) == ',')) {
            pos++;
         }
         if (pos >= param.length() || param.charAt(pos) < '0' 
                                            || param.charAt(pos) > '9') {
            return defaultValue;
         }
         while (pos < param.length() && param.charAt(pos) >= '0' 
                                          && param.charAt(pos) <= '9') {
            g = 10*g + ((int)param.charAt(pos) - (int)'0');
            pos++;
         }
         if (g > 255)
            return defaultValue;
         while (pos < param.length() && 
                  (param.charAt(pos) == ' ' || param.charAt(pos) == ',')) {
            pos++;
         }
         if (pos >= param.length() || param.charAt(pos) < '0' 
                                            || param.charAt(pos) > '9') {
            return defaultValue;
         }
         while (pos < param.length() && param.charAt(pos) >= '0' 
                                          && param.charAt(pos) <= '9') {
            b = 10*b + ((int)param.charAt(pos) - (int)'0');
            pos++;
         }
         if (b > 255)
            return defaultValue;
         return new Color(r,g,b);
      }
      else {
            // The color must be one the color names in the colorNames
            // array defined below.  Find the name in that array and
            // return the corresponding color from the color array.
            // If the name is not found, return the default color.
            
         //**** You have to complete this part ****
         
         return defaultValue;
      }
   } // end getColorParam

   private final static Color[] color = {  
                       Color.black, Color.white, 
                       Color.gray, Color.lightGray, Color.darkGray,
                       Color.red, Color.blue, Color.green,
                       Color.cyan, Color.yellow, Color.magenta,
                       Color.pink, Color.orange };

   private final static String[] colorName = { 
                       "black", "white",
                       "gray", "lightgray", "darkgray",
                       "red", "blue", "green",
                       "cyan", "yellow", "magenta",
                       "pink", "orange" };

} // end class BarChart
