Java Reference
In-Depth Information
Try It Out - Updating the Panes
We can code the setColorPane() method as:
// Set color pane label
public void setColorPane(Color color) {
String text = null; // Text for the color pane
if(color.equals(Color.RED))
text = "RED";
else if(color.equals(Color.YELLOW))
text = "YELLOW";
else if(color.equals(Color.GREEN))
text = "GREEN";
else if(color.equals(Color.BLUE))
text = "BLUE";
else
text = "CUSTOM COLOR";
colorPane.setForeground(color);
colorPane.setText(text); // Set the pane text
}
In the code for the setTypePane() method we can use switch rather than if statements to test the
parameter value because it is of type int :
// Set type pane label
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
}
Search WWH ::




Custom Search