KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rcm > awt > MultiLineString


1 /*
2  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */

30
31 package rcm.awt;
32
33 import java.awt.FontMetrics JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Label JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 // FIX: convert to RichString which supports font and color runs
39

40 public class MultiLineString {
41
42     String JavaDoc[] lines;
43
44     public MultiLineString (String JavaDoc string) {
45         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc (string, "\n");
46         lines = new String JavaDoc[tok.countTokens ()];
47         for (int i=0; i<lines.length; ++i)
48             lines[i] = tok.nextToken ();
49     }
50
51     public MultiLineString (String JavaDoc[] lines) {
52         this.lines = lines;
53     }
54
55     public int countLines () {
56         return lines.length;
57     }
58
59     public String JavaDoc getLineAt (int i) {
60         return lines[i];
61     }
62
63     public int getWidth (FontMetrics JavaDoc fm) {
64         int w = 0;
65         for (int i=0; i<lines.length; ++i)
66             w = Math.max (w, fm.stringWidth (lines[i]));
67         return w;
68     }
69
70     public int getHeight (FontMetrics JavaDoc fm) {
71         return fm.getHeight() * lines.length;
72     }
73
74     public void draw (Graphics JavaDoc g, int x, int y, int alignment) {
75         FontMetrics JavaDoc fm = g.getFontMetrics ();
76         
77         y += fm.getAscent ();
78         
79         int width = alignment != Label.LEFT
80             ? getWidth (fm)
81             : 0; // don't need it if alignment is LEFT
82

83         for (int i=0; i<lines.length; ++i) {
84             int x1 = x;
85             switch (alignment) {
86                 case Label.LEFT:
87                     break;
88                 case Label.RIGHT:
89                     x += width - fm.stringWidth (lines[i]);
90                     break;
91                 case Label.CENTER:
92                     x += (width - fm.stringWidth (lines[i]))/2;
93                     break;
94             }
95                 
96             g.drawString (lines[i], x, y);
97             y += fm.getHeight ();
98         }
99     }
100
101 }
102
Popular Tags