public int getPreferredWidth() {
switch(_shape) {
case TRIANGLE:
if (_labelWidth < _labelHeight) {
return _labelHeight << 2;
} else {
return _labelWidth << 1;
}
case OCTAGON:
if (_labelWidth < _labelHeight) {
return _label 4;
} else {
return _labe 8;
}
case RECTANGLE: default:
return _labe 8;
}
}
7.
Implement
getPreferredHeight()
, using the relative dimensions of the field label to determine the preferred
height. In the following code sample, we use a switch block to determine the preferred height based on the shape of the
custom field. For each type of shape, we use an IF statement to compare dimensions and determine the preferred height
for the custom field.
public int getPreferredHeight() {
switch(_shape) {
case TRIANGLE:
if (_labelWidth < _labelHeight) {
return _labelHeight << 1;
} else {
return _labelWidth;
}
case RECTANGLE:
return _label 4;
case OCTAGON:
return getPreferredWidth();
}
return 0;
}
8.
Implement
paint()
. The manager of a field invokes
paint()
to redraw the field when an area of the field is marked
as invalid. In the following code sample, we use a switch block to repaint a custom field based on the shape of the custom
field. For a field that has a triangle or octagon shape, we use the width of the field to calculate the horizontal and vertical
position of a lines start point and end point. We then invoke
graphics.drawLine()
and use the start and end points
to draw the lines that define the custom field. For a field that has a rectangular shape, we invoke
graphics.drawRect()
and use the width and height of the field to draw the custom field. We then invoke
graphics.drawText()
and use the width of the field to draw a string of text to an area of the field
Development Guide
UI components
25