KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Arcs_Curves > BezierAnim


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

40
41 package java2d.demos.Arcs_Curves;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.awt.image.BufferedImage JavaDoc;
47 import java.awt.geom.GeneralPath JavaDoc;
48 import javax.swing.*;
49 import java2d.AnimatingControlsSurface;
50 import java2d.CustomControls;
51
52
53 /**
54  * Animated Bezier Curve with controls for different draw & fill paints.
55  */

56 public class BezierAnim extends AnimatingControlsSurface {
57
58     private static final int NUMPTS = 6;
59     protected BasicStroke solid = new BasicStroke(10.0f,
60                         BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND);
61     protected BasicStroke dashed = new BasicStroke(10.0f,
62        BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 10, new float[] {5}, 0);
63     private float animpts[] = new float[NUMPTS * 2];
64     private float deltas[] = new float[NUMPTS * 2];
65     protected Paint fillPaint, drawPaint;
66     protected boolean doFill = true;
67     protected boolean doDraw = true;
68     protected GradientPaint gradient;
69     protected BasicStroke stroke;
70
71
72     public BezierAnim() {
73         setBackground(WHITE);
74         gradient = new GradientPaint(0,0,RED,200,200,YELLOW);
75         fillPaint = gradient;
76         drawPaint = BLUE;
77         stroke = solid;
78         setControls(new Component[] { new DemoControls(this) });
79     }
80
81
82     public void animate(float[] pts, float[] deltas, int index, int limit) {
83         float newpt = pts[index] + deltas[index];
84         if (newpt <= 0) {
85             newpt = -newpt;
86             deltas[index] = (float) (Math.random() * 4.0 + 2.0);
87         } else if (newpt >= (float) limit) {
88             newpt = 2.0f * limit - newpt;
89             deltas[index] = - (float) (Math.random() * 4.0 + 2.0);
90         }
91         pts[index] = newpt;
92     }
93
94
95     public void reset(int w, int h) {
96         for (int i = 0; i < animpts.length; i += 2) {
97             animpts[i + 0] = (float) (Math.random() * w);
98             animpts[i + 1] = (float) (Math.random() * h);
99              deltas[i + 0] = (float) (Math.random() * 6.0 + 4.0);
100              deltas[i + 1] = (float) (Math.random() * 6.0 + 4.0);
101             if (animpts[i + 0] > w / 2.0f) {
102                 deltas[i + 0] = -deltas[i + 0];
103             }
104             if (animpts[i + 1] > h / 2.0f) {
105                 deltas[i + 1] = -deltas[i + 1];
106             }
107         }
108         gradient = new GradientPaint(0,0,RED,w*.7f,h*.7f,YELLOW);
109     }
110
111
112     public void step(int w, int h) {
113         for (int i = 0; i < animpts.length; i += 2) {
114             animate(animpts, deltas, i + 0, w);
115             animate(animpts, deltas, i + 1, h);
116         }
117     }
118
119
120     public void render(int w, int h, Graphics2D g2) {
121         float[] ctrlpts = animpts;
122         int len = ctrlpts.length;
123         float prevx = ctrlpts[len - 2];
124         float prevy = ctrlpts[len - 1];
125         float curx = ctrlpts[0];
126         float cury = ctrlpts[1];
127         float midx = (curx + prevx) / 2.0f;
128         float midy = (cury + prevy) / 2.0f;
129         GeneralPath JavaDoc gp = new GeneralPath JavaDoc(GeneralPath.WIND_NON_ZERO);
130         gp.moveTo(midx, midy);
131         for (int i = 2; i <= ctrlpts.length; i += 2) {
132             float x1 = (midx + curx) / 2.0f;
133             float y1 = (midy + cury) / 2.0f;
134             prevx = curx;
135             prevy = cury;
136             if (i < ctrlpts.length) {
137                 curx = ctrlpts[i + 0];
138                 cury = ctrlpts[i + 1];
139             } else {
140                 curx = ctrlpts[0];
141                 cury = ctrlpts[1];
142             }
143             midx = (curx + prevx) / 2.0f;
144             midy = (cury + prevy) / 2.0f;
145             float x2 = (prevx + midx) / 2.0f;
146             float y2 = (prevy + midy) / 2.0f;
147             gp.curveTo(x1, y1, x2, y2, midx, midy);
148         }
149         gp.closePath();
150         if (doDraw) {
151             g2.setPaint(drawPaint);
152             g2.setStroke(stroke);
153             g2.draw(gp);
154         }
155         if (doFill) {
156             if (fillPaint instanceof GradientPaint) {
157                 fillPaint = gradient;
158             }
159             g2.setPaint(fillPaint);
160             g2.fill(gp);
161         }
162     }
163
164
165     public static void main(String JavaDoc argv[]) {
166         createDemoFrame(new BezierAnim());
167     }
168
169
170     static class DemoControls extends CustomControls implements ActionListener {
171         static TexturePaint tp1, tp2;
172         static {
173             BufferedImage JavaDoc bi = new BufferedImage JavaDoc(2,1,BufferedImage.TYPE_INT_RGB);
174             bi.setRGB(0, 0, 0xff00ff00); bi.setRGB(1, 0, 0xffff0000);
175             tp1 = new TexturePaint(bi,new Rectangle(0,0,2,1));
176             bi = new BufferedImage JavaDoc(2,1,BufferedImage.TYPE_INT_RGB);
177             bi.setRGB(0, 0, 0xff0000ff); bi.setRGB(1, 0, 0xffff0000);
178             tp2 = new TexturePaint(bi,new Rectangle(0,0,2,1));
179         }
180
181         BezierAnim demo;
182         static Paint drawPaints[] =
183                 {new Color JavaDoc(0,0,0,0), BLUE, new Color JavaDoc(0, 0, 255, 126),
184                   BLUE, tp2 };
185         static String JavaDoc drawName[] =
186                 {"No Draw", "Blue", "Blue w/ Alpha", "Blue Dash", "Texture" };
187         static Paint fillPaints[] =
188                 {new Color JavaDoc(0,0,0,0), GREEN, new Color JavaDoc(0, 255, 0, 126),
189                   tp1, new GradientPaint(0,0,RED,30,30,YELLOW) };
190         String JavaDoc fillName[] =
191                 {"No Fill", "Green", "Green w/ Alpha", "Texture", "Gradient"};
192         
193         JMenu fillMenu, drawMenu;
194         JMenuItem fillMI[] = new JMenuItem[fillPaints.length];
195         JMenuItem drawMI[] = new JMenuItem[drawPaints.length];
196         PaintedIcon fillIcons[] = new PaintedIcon[fillPaints.length];
197         PaintedIcon drawIcons[] = new PaintedIcon[drawPaints.length];
198         Font font = new Font("serif", Font.PLAIN, 10);
199
200
201         public DemoControls(BezierAnim demo) {
202             super(demo.name);
203             this.demo = demo;
204
205             JMenuBar drawMenuBar = new JMenuBar();
206             add(drawMenuBar);
207
208             JMenuBar fillMenuBar = new JMenuBar();
209             add(fillMenuBar);
210
211             drawMenu = (JMenu) drawMenuBar.add(new JMenu("Draw Choice"));
212             drawMenu.setFont(font);
213
214             for (int i = 0; i < drawPaints.length; i++) {
215                 drawIcons[i]= new PaintedIcon(drawPaints[i]);
216                 drawMI[i] = drawMenu.add(new JMenuItem(drawName[i]));
217                 drawMI[i].setFont(font);
218                 drawMI[i].setIcon(drawIcons[i]);
219                 drawMI[i].addActionListener(this);
220             }
221             drawMenu.setIcon(drawIcons[1]);
222
223             fillMenu = (JMenu) fillMenuBar.add(new JMenu("Fill Choice"));
224             fillMenu.setFont(font);
225             for (int i = 0; i < fillPaints.length; i++) {
226                 fillIcons[i]= new PaintedIcon(fillPaints[i]);
227                 fillMI[i] = fillMenu.add(new JMenuItem(fillName[i]));
228                 fillMI[i].setFont(font);
229                 fillMI[i].setIcon(fillIcons[i]);
230                 fillMI[i].addActionListener(this);
231             }
232             fillMenu.setIcon(fillIcons[fillPaints.length-1]);
233         }
234
235
236         public void actionPerformed(ActionEvent e) {
237             Object JavaDoc obj = e.getSource();
238             for (int i = 0; i < fillPaints.length; i++) {
239                 if (obj.equals(fillMI[i])) {
240                     demo.doFill = true;
241                     demo.fillPaint = fillPaints[i];
242                     fillMenu.setIcon(fillIcons[i]);
243                     break;
244                 }
245             }
246             for (int i = 0; i < drawPaints.length; i++) {
247                 if (obj.equals(drawMI[i])) {
248                     demo.doDraw = true;
249                     demo.drawPaint = drawPaints[i];
250                     if (((JMenuItem) obj).getText().endsWith("Dash")) {
251                         demo.stroke = demo.dashed;
252                     } else {
253                         demo.stroke = demo.solid;
254                     }
255                     drawMenu.setIcon(drawIcons[i]);
256                     break;
257                 }
258             }
259             if (obj.equals(fillMI[0])) {
260                 demo.doFill = false;
261             } else if (obj.equals(drawMI[0])) {
262                 demo.doDraw = false;
263             }
264             if (demo.animating.thread == null) {
265                 demo.repaint();
266             }
267         }
268
269
270         public Dimension getPreferredSize() {
271             return new Dimension(200,36);
272         }
273
274
275         public void run() {
276             Thread JavaDoc me = Thread.currentThread();
277             while (thread == me) {
278                 for (JMenuItem dmi : drawMI) {
279                     dmi.doClick();
280                     for (JMenuItem fmi : fillMI) {
281                         fmi.doClick();
282                         try {
283                             thread.sleep(3000 + (long) (Math.random() * 3000));
284                         } catch (InterruptedException JavaDoc e) { break; }
285                     }
286                 }
287             }
288             thread = null;
289         }
290
291
292         static class PaintedIcon implements Icon {
293             Paint paint;
294             public PaintedIcon(Paint p) {
295                 this.paint = p;
296             }
297     
298             public void paintIcon(Component c, Graphics g, int x, int y) {
299                 Graphics2D g2 = (Graphics2D) g;
300                 g2.setPaint(paint);
301                 g2.fillRect(x,y,getIconWidth(), getIconHeight());
302                 g2.setColor(GRAY);
303                 g2.draw3DRect(x, y, getIconWidth()-1, getIconHeight()-1, true);
304             }
305             public int getIconWidth() { return 12; }
306             public int getIconHeight() { return 12; }
307         } // End PaintedIcon class
308
} // End DemoControls class
309
} // End BezierAnim class
310
Popular Tags