Java Reference
In-Depth Information
public void actionPerformed(ActionEvent e) {
if(this == customAction) {
// This could be a new custom color
Color newColor = JColorChooser.showDialog(
SketcherFrame.this, "Select Custom Color", customColor);
if(newColor != null) {
elementColor = customColor = newColor;
// Setup small custom color icons
setCustomIconColor(CUSTOM16,customColor);
setCustomIconColor(CUSTOM24,customColor);
toolBar.repaint();
// Repaint the toolbar
}
} else {
// This is just a standard color change
elementColor = color;
}
statusBar.setColorPane(elementColor);
// Update the status bar
setChecks(colorMenu, e.getSource());
// Set Color menu checks
}
Directory "Sketcher 11 with custom colors"
If the current
Action
object is
customAction
when the method is called, you know a custom color compon-
ent initialed the event. You display the color chooser dialog using the static
showDialog()
method. The first
argument is the parent component, and you qualify
this
with
SketcherFrame
to reference the outer class
object. If the dialog returns a non-null reference, this is the new custom color. You store it in
customColor
so next time the dialog is created, this is the selected color, and in
elementColor
for new elements. Both
icons must be updated to reflect the new color, and you use the helper method that you added to
Sketcher-
Frame
for this. You then call
repaint()
for the toolbar. The toolbar is visible all the time and you must call
repaint()
to ensure it reflects the new custom color immediately.
If it's not a custom color event, you just store the current
color
member of the
ColorAction
object as
the new color. Finally you update the status bar and the Color menu checks.
Assuming you have put all that together correctly you can try it out.
TRY IT OUT: Choosing a Custom Color
Recompile Sketcher with the changes I have described. You should be able to produce something like
Figure 20-23
, but in glorious color.