import java.awt.*; import java.util.*; public class MultiLineLabel extends Canvas { public static final int LEFT = 0; // Alignment constants public static final int CENTER = 1; public static final int RIGHT = 2; protected String[] lines; // The lines of text to display protected int num_lines; // The number of lines protected int margin_width; // Left and Right margins protected int margin_height; // Top and Bottom margins protected int line_height; // Total height of the font protected int line_ascent; // Font height above baseline protected int[] line_widths; // How wide each line is protected int max_width; // The width of the widest line protected int alignment = LEFT; // The alignment of the text // This method breaks a specified label up into an array of lines. // It uses the StringTokenizer utility class. 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 max_width) max_width = line_widths[i]; } } // Here are four versions of the constructor. // Break the label up into separate lines, and save the other info. public MultiLineLabel(String label, int margin_width, int margin_height, int alignment) { newLabel(label); this.margin_width = margin_width; this.margin_height = 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 to set the various attributes of the component public void setLabel(String label) { newLabel(label); measure(); repaint(); } 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 setMarginWidth(int mw) {margin_width = mw; repaint(); } public void setMarginHeight(int mh) {margin_height = mh; repaint(); } public int getAlignment() { return alignment; } public int getMarginWidth() { return margin_width; } public int getMarginHeight() { return margin_height; } // This method is invoked after our Canvas is first created // but before it can actually be displayed. After weÕve // invoked our superclassÕs addNotify() method, we have font // metrics and can successfully call measure() to figure out // how big the label is. public void addNotify() { super.addNotify(); measure(); } // This method is called by a layout manager when it wants to // know how big weÕd like to be. public Dimension preferredSize() { return new Dimension(max_width + 2*margin_width, num_lines*line_height + 2*margin_height); } // This method is called when the layout manager it wants to know // the bare minimum amount of space we need to get by. public Dimension minimumSize() { return new Dimension(max_width, num_lines*line_height); } // This method draws the label (applets use the same method), // Note that it handles the margins and the alignment, but that // it doesnÕt have to worry about the color or font-the superclass // takes care of setting those in the Graphics object weÕre passed. 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