001    /*
002     * MultiLabel.java
003     * part of the Spirograph problem set
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
007     * CS101 homepage</a> or email <las@ai.mit.edu>.
008     *
009     * Copyright (C) 1996 Massachusetts Institute of Technology.
010     * Please do not redistribute without obtaining permission.
011     */
012    
013    package spirograph;
014    
015    import java.awt.*;
016    import java.util.*;
017    
018    /** This class creates a self-sizing Canvas that prints a multi-line
019     * String.  It can be given a specific font when created, or use a
020     * default.
021     *
022     * <p>Copyright © 1998 Massachusetts Institute of Technology<br />
023     * Copyright © 2003 Franklin W. Olin College of Engineering.</p>
024     *
025     * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu
026     * @author Henry Wong, henryw@mit.edu
027     * @author Patrick G. Heck, gus.heck@olin.edu
028     * @version $Id: MultiLabel.java,v 1.4 2003/01/15 17:36:10 gus Exp $
029     * @see CodeBox
030     */
031    public class MultiLabel extends Canvas {
032        
033        private static final int MARGINWIDTH = 10;
034        
035        private static final int MARGINHEIGHT = 10;
036    
037        private String lines[];
038        private int numLines;
039        private int maxWidth = 0;
040        private int lineHeight;
041        private int lineAscent;
042            
043        /**
044         * Create a label capable of handling multiple lines of text with a default font.
045         *
046         * @param str The textual content for the label
047         */    
048        public MultiLabel(String str) {
049            this(str, Spirograph.DEFAULTFONT);      
050        }
051    
052        /** Create a label capable of handling multiple lines of text with a specified font.
053         *
054         * @param str The textual content for the label
055         * @param f The desired font
056         */    
057        public MultiLabel(String str, Font f) {     
058            this.setFont(f);
059            // Start off parsing str and measuring stuff.
060            
061            StringTokenizer t = new StringTokenizer(str, "\n");
062            numLines = t.countTokens();         
063            lines = new String[numLines];
064            for (int i = 0; i < numLines; i++) {
065                lines[i] = t.nextToken();
066            }
067            
068            // Move on to font specific stuff.
069            
070            FontMetrics fm =this.getToolkit().getFontMetrics(this.getFont());
071            lineHeight = fm.getHeight();
072            lineAscent = fm.getAscent();
073            for (int i = 0; i < numLines; i++) {
074                if (fm.stringWidth(lines[i]) > maxWidth) {
075                    maxWidth = fm.stringWidth(lines[i]);
076                }
077            }
078            
079            this.setSize(maxWidth + 2*MultiLabel.MARGINWIDTH,
080                         lineHeight * numLines + 2*MultiLabel.MARGINHEIGHT);
081        }
082    
083        /** Overides the paint method of Canvas to do our drawing.
084         * @param g The graphics context of this component.
085         */    
086        public void paint(Graphics g) {
087            int y = MultiLabel.MARGINHEIGHT + lineAscent;
088            
089            for (int i = 0; i < numLines; i++) {
090                g.drawString(lines[i], MultiLabel.MARGINWIDTH, y);
091                y += lineHeight;
092            }
093        }
094    }    
095    
096    /*
097     * $Log: MultiLabel.java,v $
098     * Revision 1.4  2003/01/15 17:36:10  gus
099     * adding log keywords to files that don't have them
100     *
101     */