KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Mix > Stars3D


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

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 JavaDoc;
47 import javax.swing.*;
48 import java2d.ControlsSurface;
49 import java2d.CustomControls;
50
51 import static java.awt.Color JavaDoc.*;
52
53
54 /**
55  * Generate a 3D text shape with GeneralPath, render a number of small
56  * multi-colored rectangles and then render the 3D text shape.
57  */

58 public class Stars3D extends ControlsSurface {
59
60     private static Color JavaDoc 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 JavaDoc 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 JavaDoc 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; // Current point on the path
98
float tx=0, ty=0; // Transformed path point
99
float cx=0, cy=0; // Last moveTo point, for SEG_CLOSE
100
float tcx=0, tcy=0; // Transformed last moveTo point
101

102         //
103
// Iterate through the Shape and build the ribbon
104
// by adding general path objects.
105
//
106
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         } // while
219
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 JavaDoc(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 JavaDoc 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 JavaDoc ex) {}
281         }
282
283         public Dimension getPreferredSize() {
284             return new Dimension(200,37);
285         }
286
287
288         public void run() {
289             Thread JavaDoc me = Thread.currentThread();
290             try { thread.sleep(999); } catch (Exception JavaDoc e) { return; }
291             int length = getSize().width / 4;
292             int size[] = { length, length };
293             String JavaDoc 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 JavaDoc e) { return; }
303                 }
304             }
305             thread = null;
306         }
307     } // End DemoControls
308
} // End Stars3D
309
Popular Tags