Let's look at the code, which can be found in ButtonBox.java:
Class ButtonBox extends our DrawBox class and inherits the public methods: mouseDown, mouseUp, and mouseDrag. It also inherits the variables: currentX, currentY, startX, startY, and dragMode. The ButtonBox class itself adds two new variables: choice and color. The choice variable specifies the choice menu that will be added to the applet, and the color variable specifies the color in which the applet will draw its next rectangle:
public class ButtonBox extends DrawBox {
Choice choice;
Color color;
public void init() {
choice = new Choice ();
choice.addItem ("Default");
choice.addItem ("Red");
choice.addItem ("Yellow");
choice.addItem ("Blue");
color = getForeground();
add (choice);
}
g.setColor(color);
public boolean action(Event event, Object arg) {
if (event.target == choice) {
String str = (String)arg;
if (str.equals("Default"))
color = getForeground();
else if (str.equals("Red"))
color = Color.red;
else if (str.equals("Yellow"))
color = Color.yellow;
else color = Color.blue;
}
return true;
![]() |