SecondAppletpublic class SecondApplet extends FirstApplet FirstApplet is a simple applet that changes color when you click on a
Draw button. SecondApplet extends it to allow Parameters.
Depends on our ColorName class to map names to java.awt.Colors. |
Fields Summary |
---|
int | nColorsthe number of colors | Color[] | colorsThe array of Colors to use |
Methods Summary |
---|
public java.lang.String | getAppletInfo()Return information about this applet.
return "SecondApplet (Parameter Demo), Version 0\n" +
"Copyright Ian F. Darwin";
| public java.lang.String[][] | getParameterInfo()Return list of allowable parameters.
String param_info[][] = {
{"colorN", "n = 0, 1, ...", "Name or hex-tuple of color"},
};
return param_info;
| public void | init()init() is an Applet method called by the browser to initialize
super.init(); // Call FirstApplet's init() to setup the GUI.
// Now get the colors. Since we don't want the webmaster
// to have to specify both the list of colors and the name,
// we first count the colors.
for (nColors=0; ; nColors++)
if (getParameter("color" + nColors) == null)
break;
// Now we know the number, make the array
if (nColors == 0)
throw new IllegalArgumentException("SecondApplet needs colors!");
colors = new Color[nColors];
for (int i=0; i<nColors; i++)
colors[i] = ColorName.lookup(getParameter("color" + i));
| public void | paint(java.awt.Graphics g)paint() is an AWT Component method, called when the
component needs to be painted.
/* If the drawn button has been pressed, draw something */
if (requested) {
int w = getSize().width, h=getSize().height;
for (int i = 0; i<nColors; i++) {
if (colors[i] == null)
g.setColor(Color.white);
else
g.setColor(colors[i]);
g.fillRect(i*w/nColors, 0, w/nColors, h);
}
g.setColor(Color.black);
g.drawString("Welcome to Java", 50, 50);
}
|
|