1 36 37 40 41 package java2d.demos.Mix; 42 43 import java.awt.*; 44 import java.awt.event.*; 45 import java.awt.geom.*; 46 import java.awt.font.FontRenderContext ; 47 import javax.swing.*; 48 import java2d.ControlsSurface; 49 import java2d.CustomControls; 50 51 import static java.awt.Color .*; 52 53 54 58 public class Stars3D extends ControlsSurface { 59 60 private static Color colors[] = { RED, GREEN, WHITE }; 61 private static AffineTransform at = AffineTransform.getTranslateInstance(-5, -5); 62 private Shape shape, tshape; 63 private Shape ribbon; 64 protected int fontSize = 72; 65 protected String text = "Java2D"; 66 protected int numStars = 300; 67 private DemoControls controls; 68 69 70 public Stars3D() { 71 setBackground(BLACK); 72 setControls(new Component[] { new DemoControls(this) }); 73 } 74 75 76 public void render(int w, int h, Graphics2D g2) { 77 78 Rectangle2D rect = new Rectangle2D.Double(); 79 for (int i = 0; i < numStars; i++) { 80 g2.setColor(colors[i%3]); 81 g2.setComposite(AlphaComposite.getInstance( 82 AlphaComposite.SRC_OVER, (float) Math.random())); 83 rect.setRect(w*Math.random(), h*Math.random(),2,2); 84 g2.fill(rect); 85 } 86 87 FontRenderContext frc = g2.getFontRenderContext(); 88 Font font = new Font("serif.bolditalic", Font.PLAIN, fontSize); 89 shape = font.createGlyphVector(frc, text).getOutline(); 90 tshape = at.createTransformedShape(shape); 91 PathIterator pi = shape.getPathIterator(null); 92 93 float seg[] = new float[6]; 94 float tseg[] = new float[6]; 95 96 GeneralPath working = new GeneralPath(GeneralPath.WIND_NON_ZERO); 97 float x=0, y=0; float tx=0, ty=0; float cx=0, cy=0; float tcx=0, tcy=0; 102 while(!pi.isDone()) { 107 int segType = pi.currentSegment(seg); 108 switch(segType) { 109 case PathIterator.SEG_MOVETO: 110 at.transform(seg, 0, tseg, 0, 1); 111 x = seg[0]; 112 y = seg[1]; 113 tx = tseg[0]; 114 ty = tseg[1]; 115 cx = x; 116 cy = y; 117 tcx = tx; 118 tcy = ty; 119 break; 120 case PathIterator.SEG_LINETO: 121 at.transform(seg, 0, tseg, 0, 1); 122 if (Line2D.relativeCCW(x, y, tx, ty, seg[0], seg[1]) < 0) { 123 working.moveTo(x, y); 124 working.lineTo( seg[0], seg[1]); 125 working.lineTo(tseg[0], tseg[1]); 126 working.lineTo(tx, ty); 127 working.lineTo( x, y); 128 } else { 129 working.moveTo( x, y); 130 working.lineTo(tx, ty); 131 working.lineTo(tseg[0], tseg[1]); 132 working.lineTo( seg[0], seg[1]); 133 working.lineTo(x, y); 134 } 135 136 x = seg[0]; 137 y = seg[1]; 138 tx = tseg[0]; 139 ty = tseg[1]; 140 break; 141 142 case PathIterator.SEG_QUADTO: 143 at.transform(seg, 0, tseg, 0, 2); 144 if (Line2D.relativeCCW(x, y, tx, ty, seg[2], seg[3]) < 0) { 145 working.moveTo(x, y); 146 working.quadTo( seg[0], seg[1], 147 seg[2], seg[3]); 148 working.lineTo(tseg[2], tseg[3]); 149 working.quadTo(tseg[0], tseg[1], 150 tx, ty); 151 working.lineTo( x, y); 152 } else { 153 working.moveTo( x, y); 154 working.lineTo(tx, ty); 155 working.quadTo(tseg[0], tseg[1], 156 tseg[2], tseg[3]); 157 working.lineTo( seg[2], seg[3]); 158 working.quadTo( seg[0], seg[1], 159 x, y); 160 } 161 162 x = seg[2]; 163 y = seg[3]; 164 tx = tseg[2]; 165 ty = tseg[3]; 166 break; 167 168 case PathIterator.SEG_CUBICTO: 169 at.transform(seg, 0, tseg, 0, 3); 170 if (Line2D.relativeCCW(x, y, tx, ty, seg[4], seg[5]) < 0) { 171 working.moveTo(x, y); 172 working.curveTo( seg[0], seg[1], 173 seg[2], seg[3], 174 seg[4], seg[5]); 175 working.lineTo( tseg[4], tseg[5]); 176 working.curveTo(tseg[2], tseg[3], 177 tseg[0], tseg[1], 178 tx, ty); 179 working.lineTo(x, y); 180 } else { 181 working.moveTo(x, y); 182 working.lineTo(tx, ty); 183 working.curveTo(tseg[0], tseg[1], 184 tseg[2], tseg[3], 185 tseg[4], tseg[5]); 186 working.lineTo( seg[4], seg[5]); 187 working.curveTo( seg[2], seg[3], 188 seg[0], seg[1], 189 x, y); 190 } 191 192 x = seg[4]; 193 y = seg[5]; 194 tx = tseg[4]; 195 ty = tseg[5]; 196 break; 197 198 case PathIterator.SEG_CLOSE: 199 if (Line2D.relativeCCW(x, y, tx, ty, cx, cy) < 0) { 200 working.moveTo( x, y); 201 working.lineTo( cx, cy); 202 working.lineTo(tcx, tcy); 203 working.lineTo( tx, ty); 204 working.lineTo( x, y); 205 } else { 206 working.moveTo( x, y); 207 working.lineTo( tx, ty); 208 working.lineTo(tcx, tcy); 209 working.lineTo( cx, cy); 210 working.lineTo( x, y); 211 } 212 x = cx; 213 y = cy; 214 tx = tcx; 215 ty = tcy; 216 } 217 pi.next(); 218 } ribbon = working; 220 221 if (composite != null) { 222 g2.setComposite(composite); 223 } else { 224 g2.setComposite(AlphaComposite.SrcOver); 225 } 226 Rectangle r = shape.getBounds(); 227 g2.translate(w*.5-r.width*.5,h*.5+r.height*.5); 228 229 g2.setColor(BLUE); 230 g2.fill(tshape); 231 g2.setColor(new Color (255, 255, 255, 200)); 232 g2.fill(ribbon); 233 234 g2.setColor(WHITE); 235 g2.fill(shape); 236 237 g2.setColor(BLUE); 238 g2.draw(shape); 239 } 240 241 242 public static void main(String argv[]) { 243 createDemoFrame(new Stars3D()); 244 } 245 246 247 static class DemoControls extends CustomControls implements ActionListener { 248 249 Stars3D demo; 250 JTextField tf1, tf2; 251 252 public DemoControls(Stars3D demo) { 253 super(demo.name); 254 this.demo = demo; 255 JLabel l = new JLabel(" Text:"); 256 l.setForeground(BLACK); 257 add(l); 258 add(tf1 = new JTextField(demo.text)); 259 tf1.setPreferredSize(new Dimension(60,20)); 260 tf1.addActionListener(this); 261 l = new JLabel(" Size:"); 262 l.setForeground(BLACK); 263 add(l); 264 add(tf2 = new JTextField(String.valueOf(demo.fontSize))); 265 tf2.setPreferredSize(new Dimension(30,20)); 266 tf2.addActionListener(this); 267 } 268 269 public void actionPerformed(ActionEvent e) { 270 try { 271 if (e.getSource().equals(tf1)) { 272 demo.text = tf1.getText().trim(); 273 } else if (e.getSource().equals(tf2)) { 274 demo.fontSize = Integer.parseInt(tf2.getText().trim()); 275 if (demo.fontSize < 10) { 276 demo.fontSize = 10; 277 } 278 } 279 demo.repaint(); 280 } catch (Exception ex) {} 281 } 282 283 public Dimension getPreferredSize() { 284 return new Dimension(200,37); 285 } 286 287 288 public void run() { 289 Thread me = Thread.currentThread(); 290 try { thread.sleep(999); } catch (Exception e) { return; } 291 int length = getSize().width / 4; 292 int size[] = { length, length }; 293 String str[] = { "JAVA", "J2D" }; 294 while (thread == me) { 295 for (int i = 0; i < str.length; i++) { 296 demo.fontSize = size[i]; 297 tf2.setText(String.valueOf(demo.fontSize)); 298 tf1.setText(demo.text = str[i]); 299 demo.repaint(); 300 try { 301 thread.sleep(5555); 302 } catch (InterruptedException e) { return; } 303 } 304 } 305 thread = null; 306 } 307 } } | Popular Tags |