KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Lines > LineAnim


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

40
41 package java2d.demos.Lines;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.geom.*;
46 import java.awt.font.TextLayout JavaDoc;
47 import java2d.AnimatingSurface;
48
49
50 /**
51  * Lines & Paths animation illustrating BasicStroke attributes.
52  */

53 public class LineAnim extends AnimatingSurface {
54
55     private static int caps[] = { BasicStroke.CAP_BUTT,
56                 BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND};
57     private static int joins[] = { BasicStroke.JOIN_MITER,
58                 BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND};
59     private static Color JavaDoc colors[] = {GRAY, PINK, LIGHT_GRAY};
60     private static BasicStroke bs1 = new BasicStroke(1.0f);
61     private static final int CLOCKWISE = 0;
62     private static final int COUNTERCW = 1;
63
64     private Line2D lines[] = new Line2D[3];
65     private int rAmt[] = new int[lines.length];
66     private int direction[] = new int[lines.length];
67     private int speed[] = new int[lines.length];
68     private BasicStroke strokes[] = new BasicStroke[lines.length];
69     private GeneralPath path;
70     private Point2D[] pts;
71     private float size;
72     private Ellipse2D ellipse = new Ellipse2D.Double();
73
74
75     public LineAnim() {
76         setBackground(WHITE);
77     }
78
79     public void reset(int w, int h) {
80         size = (w > h) ? h/6f : w/6f;
81         for (int i = 0; i < lines.length; i++) {
82             lines[i] = new Line2D.Float(0,0,size,0);
83             strokes[i] = new BasicStroke(size/3, caps[i], joins[i]);
84             rAmt[i] = i * 360/lines.length;
85             direction[i] = i%2;
86             speed[i] = i + 1;
87         }
88
89         path = new GeneralPath();
90         path.moveTo(size, -size/2);
91         path.lineTo(size+size/2, 0);
92         path.lineTo(size, +size/2);
93
94         ellipse.setFrame(w/2-size*2-4.5f,h/2-size*2-4.5f,size*4,size*4);
95         PathIterator pi = ellipse.getPathIterator(null, 0.9);
96         Point2D[] points = new Point2D[100];
97         int num_pts = 0;
98         while ( !pi.isDone() ) {
99             float[] pt = new float[6];
100             switch ( pi.currentSegment(pt) ) {
101                 case FlatteningPathIterator.SEG_MOVETO:
102                 case FlatteningPathIterator.SEG_LINETO:
103                     points[num_pts] = new Point2D.Float(pt[0], pt[1]);
104                     num_pts++;
105             }
106             pi.next();
107         }
108         pts = new Point2D[num_pts];
109         System.arraycopy(points, 0, pts, 0, num_pts);
110     }
111
112
113     public void step(int w, int h) {
114         for (int i = 0; i < lines.length; i++) {
115             if (direction[i] == CLOCKWISE) {
116                 rAmt[i] += speed[i];
117                 if (rAmt[i] == 360) {
118                     rAmt[i] = 0;
119                 }
120             } else {
121                 rAmt[i] -= speed[i];
122                 if (rAmt[i] == 0) {
123                     rAmt[i] = 360;
124                 }
125             }
126         }
127     }
128
129
130     public void render(int w, int h, Graphics2D g2) {
131
132         ellipse.setFrame(w/2-size,h/2-size,size*2,size*2);
133         g2.setColor(BLACK);
134         g2.draw(ellipse);
135
136         for (int i = 0; i < lines.length; i++) {
137             AffineTransform at = AffineTransform.getTranslateInstance(w/2,h/2);
138             at.rotate(Math.toRadians(rAmt[i]));
139             g2.setStroke(strokes[i]);
140             g2.setColor(colors[i]);
141             g2.draw(at.createTransformedShape(lines[i]));
142             g2.draw(at.createTransformedShape(path));
143
144             int j = (int) ((double) rAmt[i]/360 * pts.length);
145             j = (j == pts.length) ? pts.length-1 : j;
146             ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9);
147             g2.fill(ellipse);
148         }
149
150         g2.setStroke(bs1);
151         g2.setColor(BLACK);
152         for (int i = 0; i < pts.length; i++) {
153             ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9);
154             g2.draw(ellipse);
155         }
156     }
157
158
159     public static void main(String JavaDoc argv[]) {
160         createDemoFrame(new LineAnim());
161     }
162 }
163
Popular Tags