Java Reference
In-Depth Information
Example 14−1: MultiLineLabel.java (continued)
**/
public class MultiLineLabel extends Canvas {
// User-specified properties
protected String label;
// The label, not broken into lines
protected int margin_width;
// Left and right margins
protected int margin_height;
// Top and bottom margins
protected Alignment alignment;
// The alignment of the text.
// Computed state values
protected int num_lines; // The number of lines
protected String[] lines; // The label, broken into lines
protected int[] line_widths; // How wide each line is
protected int max_width; // The width of the widest line
protected int line_height; // Total height of the font
protected int line_ascent; // Font height above baseline
protected boolean measured = false; // Have the lines been measured?
// Here are five versions of the constructor.
public MultiLineLabel(String label, int margin_width,
int margin_height, Alignment alignment) {
this.label = label;
// Remember all the properties.
this.margin_width = margin_width;
this.margin_height = margin_height;
this.alignment = alignment;
newLabel();
// Break the label up into lines.
}
public MultiLineLabel(String label, int margin_width, int margin_height) {
this(label, margin_width, margin_height, Alignment.LEFT);
}
public MultiLineLabel(String label, Alignment alignment) {
this(label, 10, 10, alignment);
}
public MultiLineLabel(String label) { this(label, 10, 10, Alignment.LEFT);}
public MultiLineLabel() { this(""); }
// Methods to set and query the various attributes of the component.
// Note that some query methods are inherited from the superclass.
public void setLabel(String label) {
this.label = label;
newLabel();
// Break the label into lines.
measured = false;
// Note that we need to measure lines.
repaint();
// Request a redraw.
}
public void setFont(Font f) {
super.setFont(f);
// Tell our superclass about the new font.
measured = false;
// Note that we need to remeasure lines.
repaint();
// Request a redraw.
}
public void setForeground(Color c) {
super.setForeground(c); // Tell our superclass about the new color.
repaint();
// Request a redraw (size is unchanged).
}
Search WWH ::




Custom Search