package awb; import java.awt.*; import java.util.*; /** MultiLineLabel Class * Eric Jewart * 7 January 1997 * Based on MultiLineLabel Class found in D. Flanagan's * _Java_In_A_Nutshell_. * * The MultiLineLabel displays some text like a regular * Java Label. Unlike a regular Label, it allows the * text to include \n characters. * * This class includes a large number of utility functions * which I will not document here but which are documented * in _Nutshell_. * * Variables * --------- * int LEFT * int CENTER * int RIGHT * Three different ways to align your label. * * Constructors * ------------ * MultiLineLabel(String, int, int, int) * The String is the message to be displayed. The first * int is the width of the side margins, the second is the * height of the top and bottom margins, and the third is * the alignment (MultiLineLabel.LEFT, .CENTER, or .RIGHT). * * MultiLineLabel(String, int, int) * Same as above except the alignment defaults to LEFT. * * MultiLineLabel(String, int) * Uses default margins. The int specifies alignment. * * MultiLineLabel(String) * Uses default margins and alignment. */ public class MultiLineLabel extends Canvas { public static final int LEFT = 0; public static final int CENTER = 1; public static final int RIGHT = 2; 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 = LEFT; // break label into array of lines protected void newLabel(String label) { 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(); } // figure out how large the font is, how wide each line is // and how wide the widest line is protected void measure() { try { FontMetrics fm = this.getFontMetrics(this.getFont()); 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]; } } catch (NullPointerException ex) { return; } } // four different constructors 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); } public void setText(String label) { setLabel(label); } public void setLabel(String label) { newLabel(label); measure(); repaint(); } public String getText() { String ret = ""; for (int i = 0; i < lines.length; i++) ret = ret + lines[i] + "\n"; return ret; } public void setFont(Font f) { super.setFont(f); measure(); repaint(); } public void setForeground(Color c) { super.setForeground(c); repaint(); } public void setAlignment(int a) { alignment = a; repaint(); } public void addNotify() { super.addNotify(); measure(); } public Dimension preferredSize() { return new Dimension(max_width + 2*margin_width, num_lines * line_height + 2*margin_height); } public Dimension minimumSize() { return new Dimension(max_width, num_lines*line_height); } // draw label public void paint(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); } } }