KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)Tree.java 1.28 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  * @(#)Tree.java 1.28 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.font.FontRenderContext JavaDoc;
46 import java.awt.font.TextLayout JavaDoc;
47 import java.awt.geom.AffineTransform JavaDoc;
48 import java2d.AnimatingSurface;
49
50
51 /**
52  * Transformation of characters.
53  */

54 public class Tree extends AnimatingSurface {
55
56     private char theC = 'A';
57     private Character JavaDoc theT = new Character JavaDoc(theC);
58     private Character JavaDoc theR = new Character JavaDoc((char) ((int) theC + 1));
59
60
61     public Tree() {
62         setBackground(WHITE);
63     }
64
65
66     public void reset(int w, int h) { }
67
68
69     public void step(int w, int h) {
70         setSleepAmount(4000);
71         theT = new Character JavaDoc(theC = ((char) ((int) theC + 1)));
72         theR = new Character JavaDoc((char) ((int) theC + 1));
73         if (theR.compareTo(new Character JavaDoc('z')) == 0) {
74             theC = 'A';
75         }
76     }
77
78
79     public void render(int w, int h, Graphics2D g2) {
80         int mindim = Math.min(w, h);
81         AffineTransform JavaDoc at = new AffineTransform JavaDoc();
82         at.translate((w - mindim) / 2.0,
83                      (h - mindim) / 2.0);
84         at.scale(mindim, mindim);
85         at.translate(0.5, 0.5);
86         at.scale(0.3, 0.3);
87         at.translate(-(Twidth + Rwidth), FontHeight / 4.0);
88         g2.transform(at);
89         tree(g2, mindim * 0.3, 0);
90
91     }
92
93
94     static Font theFont = new Font("serif", Font.PLAIN, 1);
95     static double Twidth = 0.6;
96     static double Rwidth = 0.6;
97     static double FontHeight = 0.75;
98     static Color JavaDoc colors[] = {BLUE,
99                              RED.darker(),
100                              GREEN.darker()};
101
102
103     public void tree(Graphics2D g2d, double size, int phase) {
104         g2d.setColor(colors[phase % 3]);
105         new TextLayout JavaDoc(theT.toString(),theFont,g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f);
106         if (size > 10.0) {
107             AffineTransform JavaDoc at = new AffineTransform JavaDoc();
108             at.setToTranslation(Twidth, -0.1);
109             at.scale(0.6, 0.6);
110             g2d.transform(at);
111             size *= 0.6;
112             new TextLayout JavaDoc(theR.toString(),theFont, g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f);
113             at.setToTranslation(Rwidth+0.75, 0);
114             g2d.transform(at);
115             Graphics2D g2dt = (Graphics2D) g2d.create();
116             at.setToRotation(-Math.PI / 2.0);
117             g2dt.transform(at);
118             tree(g2dt, size, phase + 1);
119             g2dt.dispose();
120             at.setToTranslation(.75, 0);
121             at.rotate(-Math.PI / 2.0);
122             at.scale(-1.0, 1.0);
123             at.translate(-Twidth, 0);
124             g2d.transform(at);
125             tree(g2d, size, phase);
126         }
127         g2d.setTransform(new AffineTransform JavaDoc());
128     }
129
130
131     public static void main(String JavaDoc argv[]) {
132         createDemoFrame(new Tree());
133     }
134 }
135
Popular Tags