home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / vj / VJ98 / VJProjs / webpages / appletht / Appletht.jav < prev    next >
Encoding:
Text File  |  1998-05-31  |  3.1 KB  |  117 lines

  1. import java.awt.*;
  2. import java.applet.*;
  3.  
  4. /**
  5.  * This class reads PARAM tags from its HTML host page and sets
  6.  * the color and label properties of the applet. Program execution
  7.  * begins with the init() method. 
  8.  */
  9. public class Applet1 extends Applet
  10. {
  11.     /**
  12.      * The entry point for the applet. 
  13.      */
  14.     public void init()
  15.     {
  16.         initForm();
  17.  
  18.         usePageParams();
  19.  
  20.         // TODO: Add any constructor code after initForm call.
  21.     }
  22.  
  23.     private    final String labelParam = "label";
  24.     private    final String backgroundParam = "background";
  25.     private    final String foregroundParam = "foreground";
  26.  
  27.     /**
  28.      * Reads parameters from the applet's HTML host and sets applet
  29.      * properties.
  30.      */
  31.     private void usePageParams()
  32.     {
  33.         final String defaultLabel = "Default label";
  34.         final String defaultBackground = "C0C0C0";
  35.         final String defaultForeground = "000000";
  36.         String labelValue;
  37.         String backgroundValue;
  38.         String foregroundValue;
  39.  
  40.         /** 
  41.          * Read the <PARAM NAME="label" VALUE="some string">,
  42.          * <PARAM NAME="background" VALUE="rrggbb">,
  43.          * and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
  44.          * the applet's HTML host.
  45.          */
  46.         labelValue = getParameter(labelParam);
  47.         backgroundValue = getParameter(backgroundParam);
  48.         foregroundValue = getParameter(foregroundParam);
  49.  
  50.         if ((labelValue == null) || (backgroundValue == null) ||
  51.             (foregroundValue == null))
  52.         {
  53.             /**
  54.              * There was something wrong with the HTML host tags.
  55.              * Generate default values.
  56.              */
  57.             labelValue = defaultLabel;
  58.             backgroundValue = defaultBackground;
  59.             foregroundValue = defaultForeground;
  60.         }
  61.  
  62.         /**
  63.          * Set the applet's string label, background color, and
  64.          * foreground colors.
  65.          */
  66.         label1.setText(labelValue);
  67.         label1.setBackground(stringToColor(backgroundValue));
  68.         label1.setForeground(stringToColor(foregroundValue));
  69.         this.setBackground(stringToColor(backgroundValue));
  70.         this.setForeground(stringToColor(foregroundValue));
  71.     }
  72.  
  73.     /**
  74.      * Converts a string formatted as "rrggbb" to an awt.Color object
  75.      */
  76.     private Color stringToColor(String paramValue)
  77.     {
  78.         int red;
  79.         int green;
  80.         int blue;
  81.  
  82.         red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
  83.         green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
  84.         blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
  85.  
  86.         return new Color(red,green,blue);
  87.     }
  88.  
  89.     /**
  90.      * External interface used by design tools to show properties of an applet.
  91.      */
  92.     public String[][] getParameterInfo()
  93.     {
  94.         String[][] info =
  95.         {
  96.             { labelParam, "String", "Label string to be displayed" },
  97.             { backgroundParam, "String", "Background color, format \"rrggbb\"" },
  98.             { foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
  99.         };
  100.         return info;
  101.     }
  102.  
  103.     Label label1 = new Label();
  104.  
  105.     /**
  106.      * Intializes values for the applet and its components
  107.      */
  108.     void initForm()
  109.     {
  110.         this.setBackground(Color.lightGray);
  111.         this.setForeground(Color.black);
  112.         label1.setText("label1");
  113.         this.setLayout(new BorderLayout());
  114.         this.add("North",label1);
  115.     }
  116. }
  117.