KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BezierAnimationPanel


1 /*
2  * @(#)BezierAnimationPanel.java 1.16 05/11/17
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  * @(#)BezierAnimationPanel.java 1.16 05/11/17
39  */

40
41
42 import javax.swing.*;
43 import javax.swing.event.*;
44 import javax.swing.text.*;
45 import javax.swing.border.*;
46 import javax.swing.colorchooser.*;
47 import javax.swing.filechooser.*;
48 import javax.accessibility.*;
49  
50 import java.awt.*;
51 import java.awt.font.*;
52 import java.awt.geom.*;
53 import java.awt.image.*;
54 import java.awt.event.*;
55
56 /**
57  * BezierAnimationPanel
58  *
59  * @version 1.16 11/17/05
60  * @author Jim Graham
61  * @author Jeff Dinkins (removed dynamic setting changes, made swing friendly)
62  */

63 class BezierAnimationPanel extends JPanel implements Runnable JavaDoc {
64     
65     Color backgroundColor = new Color(0, 0, 153);
66     Color outerColor = new Color(255, 255, 255);
67     Color gradientColorA = new Color(255, 0, 101);
68     Color gradientColorB = new Color(255, 255, 0);
69
70     boolean bgChanged = false;
71
72     GradientPaint gradient = null;
73     
74     public final int NUMPTS = 6;
75
76     float animpts[] = new float[NUMPTS * 2];
77
78     float deltas[] = new float[NUMPTS * 2];
79
80     float staticpts[] = {
81      50.0f, 0.0f,
82     150.0f, 0.0f,
83     200.0f, 75.0f,
84     150.0f, 150.0f,
85      50.0f, 150.0f,
86       0.0f, 75.0f,
87     };
88
89     float movepts[] = new float[staticpts.length];
90
91     BufferedImage img;
92
93     Rectangle bounds = null;
94
95     Thread JavaDoc anim;
96     
97     private final Object JavaDoc lock = new Object JavaDoc();
98
99     /**
100      * BezierAnimationPanel Constructor
101      */

102     public BezierAnimationPanel() {
103         addHierarchyListener(
104         new HierarchyListener() {
105            public void hierarchyChanged(HierarchyEvent e) {
106            if(isShowing()) {
107                start();
108            } else {
109                stop();
110            }
111            }
112        }
113     );
114     setBackground(getBackgroundColor());
115     }
116
117     public boolean isOpaque() {
118         return true;
119     }
120
121     public Color getGradientColorA() {
122     return gradientColorA;
123     }
124
125     public void setGradientColorA(Color c) {
126     if(c != null) {
127         gradientColorA = c;
128     }
129     }
130
131     public Color getGradientColorB() {
132     return gradientColorB;
133     }
134
135     public void setGradientColorB(Color c) {
136     if(c != null) {
137         gradientColorB = c;
138     }
139     }
140
141     public Color getOuterColor() {
142     return outerColor;
143     }
144
145     public void setOuterColor(Color c) {
146     if(c != null) {
147         outerColor = c;
148     }
149     }
150
151     public Color getBackgroundColor() {
152     return backgroundColor;
153     }
154
155     public void setBackgroundColor(Color c) {
156     if(c != null) {
157         backgroundColor = c;
158         setBackground(c);
159         bgChanged = true;
160     }
161     }
162
163     public void start() {
164     Dimension size = getSize();
165     for (int i = 0; i < animpts.length; i += 2) {
166         animpts[i + 0] = (float) (Math.random() * size.width);
167         animpts[i + 1] = (float) (Math.random() * size.height);
168         deltas[i + 0] = (float) (Math.random() * 4.0 + 2.0);
169         deltas[i + 1] = (float) (Math.random() * 4.0 + 2.0);
170         if (animpts[i + 0] > size.width / 6.0f) {
171         deltas[i + 0] = -deltas[i + 0];
172         }
173         if (animpts[i + 1] > size.height / 6.0f) {
174         deltas[i + 1] = -deltas[i + 1];
175         }
176     }
177     anim = new Thread JavaDoc(this);
178     anim.setPriority(Thread.MIN_PRIORITY);
179     anim.start();
180     }
181
182     public synchronized void stop() {
183     anim = null;
184     notify();
185     }
186
187     public void animate(float[] pts, float[] deltas, int index, int limit) {
188     float newpt = pts[index] + deltas[index];
189     if (newpt <= 0) {
190         newpt = -newpt;
191         deltas[index] = (float) (Math.random() * 3.0 + 2.0);
192     } else if (newpt >= (float) limit) {
193         newpt = 2.0f * limit - newpt;
194         deltas[index] = - (float) (Math.random() * 3.0 + 2.0);
195     }
196     pts[index] = newpt;
197     }
198
199     public void run() {
200     Thread JavaDoc me = Thread.currentThread();
201     while (getSize().width <= 0) {
202         try {
203         anim.sleep(500);
204         } catch (InterruptedException JavaDoc e) {
205         return;
206         }
207         }
208          
209     Graphics2D g2d = null;
210     Graphics2D BufferG2D = null;
211     Graphics2D ScreenG2D = null;
212     BasicStroke solid = new BasicStroke(9.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 9.0f);
213     GeneralPath gp = new GeneralPath(GeneralPath.WIND_NON_ZERO);
214     int rule = AlphaComposite.SRC_OVER;
215     AlphaComposite opaque = AlphaComposite.SrcOver;
216     AlphaComposite blend = AlphaComposite.getInstance(rule, 0.9f);
217     AlphaComposite set = AlphaComposite.Src;
218     int frame = 0;
219     int frametmp = 0;
220     Dimension oldSize = getSize();
221     Shape clippath = null;
222     while (anim == me) {
223         Dimension size = getSize();
224         if (size.width != oldSize.width || size.height != oldSize.height) {
225         img = null;
226         clippath = null;
227         if (BufferG2D != null) {
228             BufferG2D.dispose();
229             BufferG2D = null;
230         }
231         if (ScreenG2D != null) {
232             ScreenG2D.dispose();
233             ScreenG2D = null;
234         }
235         }
236         oldSize = size;
237
238         if (img == null) {
239         img = (BufferedImage) createImage(size.width, size.height);
240         }
241
242         if (BufferG2D == null) {
243         BufferG2D = img.createGraphics();
244         BufferG2D.setRenderingHint(RenderingHints.KEY_RENDERING,
245                        RenderingHints.VALUE_RENDER_DEFAULT);
246         BufferG2D.setClip(clippath);
247         }
248         g2d = BufferG2D;
249
250         float[] ctrlpts;
251         for (int i = 0; i < animpts.length; i += 2) {
252         animate(animpts, deltas, i + 0, size.width);
253         animate(animpts, deltas, i + 1, size.height);
254         }
255         ctrlpts = animpts;
256         int len = ctrlpts.length;
257         gp.reset();
258         int dir = 0;
259         float prevx = ctrlpts[len - 2];
260         float prevy = ctrlpts[len - 1];
261         float curx = ctrlpts[0];
262         float cury = ctrlpts[1];
263         float midx = (curx + prevx) / 2.0f;
264         float midy = (cury + prevy) / 2.0f;
265         gp.moveTo(midx, midy);
266         for (int i = 2; i <= ctrlpts.length; i += 2) {
267         float x1 = (midx + curx) / 2.0f;
268         float y1 = (midy + cury) / 2.0f;
269         prevx = curx;
270         prevy = cury;
271         if (i < ctrlpts.length) {
272             curx = ctrlpts[i + 0];
273             cury = ctrlpts[i + 1];
274         } else {
275             curx = ctrlpts[0];
276             cury = ctrlpts[1];
277         }
278         midx = (curx + prevx) / 2.0f;
279         midy = (cury + prevy) / 2.0f;
280         float x2 = (prevx + midx) / 2.0f;
281         float y2 = (prevy + midy) / 2.0f;
282         gp.curveTo(x1, y1, x2, y2, midx, midy);
283         }
284         gp.closePath();
285
286         synchronized(lock) {
287         g2d.setComposite(set);
288         g2d.setBackground(backgroundColor);
289         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
290                  RenderingHints.VALUE_ANTIALIAS_OFF);
291
292         if(bgChanged || bounds == null) {
293         bounds = new Rectangle(0, 0, getWidth(), getHeight());
294         bgChanged = false;
295         }
296         
297         // g2d.clearRect(bounds.x-5, bounds.y-5, bounds.x + bounds.width + 5, bounds.y + bounds.height + 5);
298
g2d.clearRect(0, 0, getWidth(), getHeight());
299
300         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
301                  RenderingHints.VALUE_ANTIALIAS_ON);
302         g2d.setColor(outerColor);
303         g2d.setComposite(opaque);
304         g2d.setStroke(solid);
305         g2d.draw(gp);
306         g2d.setPaint(gradient);
307
308         if(!bgChanged) {
309         bounds = gp.getBounds();
310         } else {
311         bounds = new Rectangle(0, 0, getWidth(), getHeight());
312         bgChanged = false;
313         }
314         gradient = new GradientPaint(bounds.x, bounds.y, gradientColorA,
315                      bounds.x + bounds.width, bounds.y + bounds.height,
316                      gradientColorB, true);
317         g2d.setComposite(blend);
318         g2d.fill(gp);
319         }
320         if (g2d == BufferG2D) {
321         repaint();
322         }
323         ++frame;
324         Thread.yield();
325     }
326     if (g2d != null) {
327         g2d.dispose();
328     }
329     }
330
331     public void paint(Graphics g) {
332     synchronized (lock) {
333        Graphics2D g2d = (Graphics2D) g;
334        if (img != null) {
335            int imgw = img.getWidth();
336            int imgh = img.getHeight();
337            g2d.setComposite(AlphaComposite.Src);
338            g2d.drawImage(img, null, 0, 0);
339        }
340         }
341     }
342 }
343
Popular Tags