KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)Outline.java 1.29 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  * @(#)Outline.java 1.29 06/08/29
39  */

40
41 package java2d.demos.Fonts;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.awt.geom.AffineTransform JavaDoc;
47 import java.awt.font.TextAttribute JavaDoc;
48 import java.awt.font.TextLayout JavaDoc;
49 import java.awt.font.FontRenderContext JavaDoc;
50 import java.text.AttributedString JavaDoc;
51 import java.text.AttributedCharacterIterator JavaDoc;
52 import java2d.Surface;
53
54
55 /**
56  * Rendering text as an outline shape.
57  */

58 public class Outline extends Surface {
59
60     public Outline() {
61         setBackground(WHITE);
62     }
63
64
65     public void render(int w, int h, Graphics2D g2) {
66
67         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
68         Font f = new Font("sansserif",Font.PLAIN,w/8);
69         Font f1 = new Font("sansserif",Font.ITALIC,w/8);
70         String JavaDoc s = "AttributedString";
71         AttributedString JavaDoc as = new AttributedString JavaDoc(s);
72         as.addAttribute(TextAttribute.FONT, f, 0, 10 );
73         as.addAttribute(TextAttribute.FONT, f1, 10, s.length() );
74         AttributedCharacterIterator JavaDoc aci = as.getIterator();
75         TextLayout JavaDoc tl = new TextLayout JavaDoc (aci, frc);
76         float sw = (float) tl.getBounds().getWidth();
77         float sh = (float) tl.getBounds().getHeight();
78         Shape sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2, h*0.2+sh/2));
79         g2.setColor(BLUE);
80         g2.setStroke(new BasicStroke(1.5f));
81         g2.draw(sha);
82         g2.setColor(MAGENTA);
83         g2.fill(sha);
84
85         f = new Font("serif", Font.BOLD,w/6);
86         tl = new TextLayout JavaDoc("Outline", f, frc);
87         sw = (float) tl.getBounds().getWidth();
88         sh = (float) tl.getBounds().getHeight();
89         sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2,h*0.5+sh/2));
90         g2.setColor(BLACK);
91         g2.draw(sha);
92         g2.setColor(RED);
93         g2.fill(sha);
94
95         f = new Font("sansserif",Font.ITALIC,w/8);
96         AffineTransform JavaDoc fontAT = new AffineTransform JavaDoc();
97         fontAT.shear(-0.2, 0.0);
98         Font derivedFont = f.deriveFont(fontAT);
99         tl = new TextLayout JavaDoc("Italic-Shear", derivedFont, frc);
100         sw = (float) tl.getBounds().getWidth();
101         sh = (float) tl.getBounds().getHeight();
102         sha = tl.getOutline(AffineTransform.getTranslateInstance(w/2-sw/2,h*0.80f+sh/2));
103         g2.setColor(GREEN);
104         g2.draw(sha);
105         g2.setColor(BLACK);
106         g2.fill(sha);
107     }
108
109
110     public static void main(String JavaDoc s[]) {
111         createDemoFrame(new Outline());
112     }
113 }
114
Popular Tags