import java.awt.*; import java.applet.*; /** * This class reads PARAM tags from its HTML host page and sets * the color and label properties of the applet. Program execution * begins with the init() method. */ public class Applet1 extends Applet { /** * The entry point for the applet. */ private Font font; private String labelValue; private String backgroundValue; private String foregroundValue; public void init() { usePageParams(); font = new Font("Helvetica",Font.BOLD, 48); } private final String labelParam = "label"; private final String backgroundParam = "background"; private final String foregroundParam = "foreground"; /** * Reads parameters from the applet's HTML host and sets applet * properties. */ private void usePageParams() { final String defaultLabel = "Greg Loves Microsoft"; final String defaultBackground = "C0C0C0"; final String defaultForeground = "000000"; /** * Read the , * , * and tags from * the applet's HTML host. */ labelValue = getParameter(labelParam); backgroundValue = getParameter(backgroundParam); foregroundValue = getParameter(foregroundParam); if ((labelValue == null) || (backgroundValue == null) || (foregroundValue == null)) { /** * There was something wrong with the HTML host tags. * Generate default values. */ labelValue = defaultLabel; backgroundValue = defaultBackground; foregroundValue = defaultForeground; } /** * Set the applet's string label, background color, and * foreground colors. */ this.setBackground(stringToColor(backgroundValue)); this.setForeground(stringToColor(foregroundValue)); } /** * Converts a string formatted as "rrggbb" to an awt.Color object */ private Color stringToColor(String paramValue) { int red; int green; int blue; red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue(); green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue(); blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue(); return new Color(red,green,blue); } /** * External interface used by design tools to show properties of an applet. */ public String[][] getParameterInfo() { String[][] info = { { labelParam, "String", "Label string to be displayed" }, { backgroundParam, "String", "Background color, format \"rrggbb\"" }, { foregroundParam, "String", "Foreground color, format \"rrggbb\"" }, }; return info; } public void paint (Graphics g) { g.setColor(Color.blue); g.fillOval(10,10,330,100); g.setColor(Color.red); g.drawOval(10,10,330,100); g.drawOval(9,9,332,102); g.drawOval(8,8,334,104); g.drawOval(7,7,336,106); /* set the text color to the HTML param tag */ g.setColor(stringToColor(foregroundValue)); g.setFont(font); /* draw the string that has been assigned from the html param tag */ g.drawString(this.labelValue, 40,75); } }