FileDocCategorySizeDatePackage
MultiLineLabel.javaAPI DocExample5371Fri Feb 16 13:29:32 GMT 1996None

MultiLineLabel

public class MultiLineLabel extends Canvas

Fields Summary
public static final int
LEFT
public static final int
CENTER
public static final int
RIGHT
protected String[]
lines
protected int
num_lines
protected int
margin_width
protected int
margin_height
protected int
line_height
protected int
line_ascent
protected int[]
line_widths
protected int
max_width
protected int
alignment
Constructors Summary
public MultiLineLabel(String label, int margin_width, int margin_height, int alignment)

        newLabel(label);
        this.margin_width = margin_width;
        this.margin_height = margin_height;
        this.alignment = alignment;
    
public MultiLineLabel(String label, int margin_width, int margin_height)

        this(label, margin_width, margin_height, LEFT);
    
public MultiLineLabel(String label, int alignment)

        this(label, 10, 10, alignment);
    
public MultiLineLabel(String label)

        this(label, 10, 10, LEFT);
    
Methods Summary
public voidaddNotify()

 super.addNotify(); measure(); 
public intgetAlignment()

 return alignment; 
public intgetMarginHeight()

 return margin_height; 
public intgetMarginWidth()

 return margin_width; 
protected voidmeasure()

        FontMetrics fm = this.getFontMetrics(this.getFont());
        // If we don't have font metrics yet, just return.
        if (fm == null) return;
        
        line_height = fm.getHeight();
        line_ascent = fm.getAscent();
        max_width = 0;
        for(int i = 0; i < num_lines; i++) {
            line_widths[i] = fm.stringWidth(lines[i]);
            if (line_widths[i] > max_width) max_width = line_widths[i];
        }
    
public java.awt.DimensionminimumSize()

        return new Dimension(max_width, num_lines * line_height);
    
protected voidnewLabel(java.lang.String label)

   // The alignment of the text.
    
    // This method breaks a specified label up into an array of lines.
    // It uses the StringTokenizer utility class.
        
        StringTokenizer t = new StringTokenizer(label, "\n");
        num_lines = t.countTokens();
        lines = new String[num_lines];
        line_widths = new int[num_lines];
        for(int i = 0; i < num_lines; i++) lines[i] = t.nextToken();
    
public voidpaint(java.awt.Graphics g)

        int x, y;
        Dimension d = this.size();
        y = line_ascent + (d.height - num_lines * line_height)/2;
        for(int i = 0; i < num_lines; i++, y += line_height) {
            switch(alignment) {
            case LEFT:
                x = margin_width; break;
            case CENTER:
            default:
                x = (d.width - line_widths[i])/2; break;
            case RIGHT:
                x = d.width - margin_width - line_widths[i]; break;
            }
            g.drawString(lines[i], x, y);
        }
    
public java.awt.DimensionpreferredSize()

        return new Dimension(max_width + 2*margin_width, 
                     num_lines * line_height + 2*margin_height);
    
public voidsetAlignment(int a)

 alignment = a; repaint(); 
public voidsetFont(java.awt.Font f)

        super.setFont(f);
        measure();
        repaint();
    
public voidsetForeground(java.awt.Color c)

 
        super.setForeground(c); 
        repaint(); 
    
public voidsetLabel(java.lang.String label)

        newLabel(label);
        measure();
        repaint();
    
public voidsetMarginHeight(int mh)

 margin_height = mh; repaint(); 
public voidsetMarginWidth(int mw)

 margin_width = mw; repaint();