KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Fonts > AttributedStr


1 /*
2  * @(#)AttributedStr.java 1.15 06/08/29
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)AttributedStr.java 1.15 06/08/29
39  */

40
41 package java2d.demos.Fonts;
42
43 import java.awt.*;
44 import java.awt.font.*;
45 import java.awt.geom.*;
46 import java.net.URL JavaDoc;
47 import java.io.File JavaDoc;
48 import java.io.FileInputStream JavaDoc;
49 import java.io.InputStream JavaDoc;
50 import java.awt.image.BufferedImage JavaDoc;
51 import java.text.AttributedString JavaDoc;
52 import java.text.AttributedCharacterIterator JavaDoc;
53 import java2d.Surface;
54
55 import static java.awt.Font JavaDoc.*;
56 import static java.awt.font.TextAttribute JavaDoc.*;
57
58
59 /**
60  * Demonstrates how to build an AttributedString and then render the
61  * string broken over lines.
62  */

63 public class AttributedStr extends Surface {
64
65     static Color black = new Color( 20, 20, 20);
66     static Color blue = new Color( 94, 105, 176);
67     static Color yellow = new Color(255, 255, 140);
68     static Color red = new Color(149, 43, 42);
69     static Color white = new Color(240, 240, 255);
70     static String JavaDoc text = " A quick brown fox jumped over the lazy duke ";
71     static AttributedString JavaDoc as = new AttributedString JavaDoc(text);
72     static AttributedCharacterIterator JavaDoc aci;
73     static {
74         Shape shape = new Ellipse2D.Double(0,25,12,12);
75         ShapeGraphicAttribute sga = new
76             ShapeGraphicAttribute(shape, GraphicAttribute.TOP_ALIGNMENT, false);
77         as.addAttribute(CHAR_REPLACEMENT, sga, 0, 1);
78
79
80         Font JavaDoc font = new Font JavaDoc("sanserif", BOLD|ITALIC, 20);
81         int index = text.indexOf("quick");
82         as.addAttribute(FONT, font, index, index+5);
83
84         index = text.indexOf("brown");
85         font = new Font JavaDoc("serif", BOLD, 20);
86         as.addAttribute(FONT, font, index, index+5);
87         as.addAttribute(FOREGROUND, red, index, index+5);
88
89         index = text.indexOf("fox");
90         AffineTransform fontAT = new AffineTransform();
91         fontAT.rotate(Math.toRadians(10));
92         Font JavaDoc fx = new Font JavaDoc("serif", BOLD, 30).deriveFont(fontAT);
93         as.addAttribute(FONT, fx, index, index+1);
94         as.addAttribute(FONT, fx, index+1, index+2);
95         as.addAttribute(FONT, fx, index+2, index+3);
96
97         fontAT.setToRotation(Math.toRadians(-4));
98         fx = font.deriveFont(fontAT);
99         index = text.indexOf("jumped");
100         as.addAttribute(FONT, fx, index, index+6);
101
102         font = new Font JavaDoc("serif", BOLD|ITALIC, 30);
103         index = text.indexOf("over");
104         as.addAttribute(UNDERLINE, UNDERLINE_ON, index, index+4);
105         as.addAttribute(FOREGROUND, white, index, index+4);
106         as.addAttribute(FONT, font, index, text.length());
107
108         font = new Font JavaDoc("dialog", PLAIN, 20);
109         int i = text.indexOf("duke");
110         as.addAttribute(FONT, font, index, i-1);
111
112         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(4,4,BufferedImage.TYPE_INT_ARGB);
113         bi.setRGB(0, 0, 0xffffffff);
114         TexturePaint tp = new TexturePaint(bi,new Rectangle(0,0,4,4));
115         as.addAttribute(BACKGROUND, tp, i, i+4);
116         font = new Font JavaDoc("serif", BOLD, 40);
117         as.addAttribute(FONT, font, i, i+4);
118     }
119
120
121     public AttributedStr() {
122         setBackground(Color.white);
123
124         Font JavaDoc font = getFont("A.ttf");
125         if (font != null) {
126             font = font.deriveFont(PLAIN, 70);
127         } else {
128             font = new Font JavaDoc("serif", PLAIN, 50);
129         }
130         int index = text.indexOf("A")+1;
131         as.addAttribute(FONT, font, 0, index);
132         as.addAttribute(FOREGROUND, white, 0, index);
133
134         font = new Font JavaDoc("dialog", PLAIN, 40);
135         int size = getFontMetrics(font).getHeight();
136         BufferedImage JavaDoc bi =
137                 new BufferedImage JavaDoc(size, size, BufferedImage.TYPE_INT_ARGB);
138         Graphics2D big = bi.createGraphics();
139         big.drawImage(getImage("snooze.gif"), 0, 0, size, size, null);
140         ImageGraphicAttribute iga =
141                 new ImageGraphicAttribute(bi, GraphicAttribute.TOP_ALIGNMENT);
142         as.addAttribute(CHAR_REPLACEMENT, iga, text.length()-1, text.length());
143
144         aci = as.getIterator();
145     }
146
147
148     public void render(int w, int h, Graphics2D g2) {
149
150         float x = 5, y = 0;
151         FontRenderContext frc = g2.getFontRenderContext();
152         LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
153
154         g2.setPaint(new GradientPaint(0,h,blue,w,0,black));
155         g2.fillRect(0, 0, w, h);
156
157         g2.setColor(white);
158         String JavaDoc s = "AttributedString LineBreakMeasurer";
159         Font JavaDoc font = new Font JavaDoc("serif", PLAIN, 12);
160         TextLayout tl = new TextLayout(s, font, frc);
161         
162         tl.draw(g2, 5, y += (float) tl.getBounds().getHeight());
163
164         g2.setColor(yellow);
165
166         while (y < h-tl.getAscent()) {
167             lbm.setPosition(0);
168             while (lbm.getPosition() < text.length()) {
169                 tl = lbm.nextLayout(w-x);
170                 if (!tl.isLeftToRight()) {
171                     x = w - tl.getAdvance();
172                 }
173                 tl.draw(g2, x, y += tl.getAscent());
174                 y += tl.getDescent() + tl.getLeading();
175             }
176         }
177     }
178
179
180     public static void main(String JavaDoc s[]) {
181         createDemoFrame(new AttributedStr());
182     }
183 }
184
Popular Tags