Java Reference
In-Depth Information
} else if(color.equals(Color.GREEN)) {
text = "GREEN";
icon = GREEN16;
} else if(color.equals(Color.BLUE)) {
text = "BLUE";
icon = BLUE16;
} else {
text = "CUSTOM COLOR";
}
colorPane.setIcon(icon);
colorPane.setText(text);
// Set the pane text
}
Directory "Sketcher 1 with a status bar"
This sets the text and selects an icon for a color pane based on the
Color
argument that is passed to it.
The icons are available from the
SketcherConstants
class and these are imported statically so you can
reference them without qualification. The text and icon to be displayed in the color pane are selected in the
series of
if-else
statements. They each compare the color that is passed as the argument with the standard
colors you use in Sketcher and set the
text
and
icon
variables accordingly. The last
else
should never be
reached at the moment, but it is obvious if it is. This provides the possibility of adding more flexibility in
the choice of drawing color later on. The
setText()
and
setIcon()
methods that set the text and icon to
be displayed are inherited from the
JLabel
class.
In the code for the
setTypePane()
method, you can use a
switch
statement rather than
if
statements to
test the parameter value because it is of type
int
:
public void setTypePane(int elementType) {
String text = null;
// Text for the type pane
switch(elementType) {
case LINE:
text = "LINE";
break;
case RECTANGLE:
text = "RECTANGLE";
break;
case CIRCLE:
text = "CIRCLE";
break;
case CURVE:
text = "CURVE";
break;
default:
assert false;
}
typePane.setText(text);
// Set the pane text
}