1 36 37 40 41 package java2d.demos.Fonts; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.font.FontRenderContext ; 46 import java.awt.font.TextLayout ; 47 import java.awt.geom.AffineTransform ; 48 import java2d.AnimatingSurface; 49 50 51 54 public class Tree extends AnimatingSurface { 55 56 private char theC = 'A'; 57 private Character theT = new Character (theC); 58 private Character theR = new Character ((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 (theC = ((char) ((int) theC + 1))); 72 theR = new Character ((char) ((int) theC + 1)); 73 if (theR.compareTo(new Character ('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 at = new AffineTransform (); 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 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 (theT.toString(),theFont,g2d.getFontRenderContext()).draw(g2d, 0.0f, 0.0f); 106 if (size > 10.0) { 107 AffineTransform at = new AffineTransform (); 108 at.setToTranslation(Twidth, -0.1); 109 at.scale(0.6, 0.6); 110 g2d.transform(at); 111 size *= 0.6; 112 new TextLayout (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 ()); 128 } 129 130 131 public static void main(String argv[]) { 132 createDemoFrame(new Tree()); 133 } 134 } 135 | Popular Tags |