Motorola T720 Developer's Manual page 45

Hide thumbs Also See for T720:
Table of Contents

Advertisement

Coding Example
public class RoundButton extends Button {
String label;
/**
* Constructs a RoundButton with no label.
*/
public RoundButton() {
}
/**
* Constructs a RoundButton with the specified label.
* @param label the label of the button
*/
public RoundButton(String label) {
int x, y;
}
Thus far, this code looks very standard. It defines a RoundButton class that extends the
Button. If you look at the LWT JavaDoc documentation for the Button class, you can see
that this section of code defines and implements the methods RoundButton() and
RoundButton(String label), similar to those in the Button class. The empty label version of
the RoundButton is trivial, while the labeled implementation does nothing out of the
ordinary.
The method RoundButton(String label) uses the LWT Component
methods getPreferredWidth() and getPreferredHeight() to obtain
the this button's dimensions in pixels. These two methods take into account the size of the
string that comprises the RoundButton's label.
The LWT Component methods setBottomEdge() and setRightEdge()
adjust RoundButton's height and width, respectively. Because the label string's width is
typically larger than the its height, the width value x is plugged into the
setBottomEdge() method as well as setRightEdge(). This ensures that
RoundButton's paint() methods draws a circle—and not an oval. The code then
signals the LWT that the button's dimensions have changed with the
preferredWidthChanged() and preferredHeightChanged()
methods. Finally, the method invokes repaint() to force a screen update so that the
top-level container object, ComponentScreen, draws the new RoundButton. Moving
through the file with the editor, we see:
this("");
this.label = label;
x = getPreferredWidth();
y = getPreferredHeight();
setBottomEdge(HEIGHT, x);
setRightEdge(WIDTH, x);
preferredWidthChanged();
preferredHeightChanged();
repaint();
// The Button's text
// note use of width
45

Advertisement

Table of Contents
loading

Table of Contents