Java Reference
In-Depth Information
}
Directory "Sketcher 6 using Action objects"
Add this definition to the
SketcherFrame
class following the previous inner class. In this class you retain
the
typeID
to identify the element type. This makes the listener operation simple and fast. Because each ob-
ject corresponds to a particular element type, there is no need for any testing of the event in the
actionPer-
formed()
method — you just store the
typeID
as the new element type in the
elementType
member of the
SketcherFrame
class.
Add the following statement to the
SketcherFrame
class to define members that store references to
TypeAction
objects, following the statement that defines the members storing
FileAction
references:
// Element type actions
private TypeAction lineAction, rectangleAction, circleAction, curveAction;
private TypeAction[] typeActions;
// Type actions as an array
Directory "Sketcher 6 using Action objects"
You have a convenience array here for
Action
objects, too.
Actions for Color Menu Items
The third inner class to
SketcherFrame
defining
Action
objects for the Color menu items is just as simple:
// Handles color menu items
class ColorAction extends AbstractAction {
// Create an action with a name and a color
public ColorAction(String name, Color color) {
super(name);
this.color = color;
}
// Create an action with a name, a color, and an accelerator
public ColorAction(String name, Color color, char ch, int modifiers) {
this(name, color);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(ch, modifiers));
// Now find the character to underline
int index = name.toUpperCase().indexOf(ch);
if(index != -1) {
putValue(DISPLAYED_MNEMONIC_INDEX_KEY, index);
}
}