KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > renderer > FanRenderer


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.animation.renderer;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics2D JavaDoc;
35 import java.awt.Shape JavaDoc;
36 import java.awt.geom.GeneralPath JavaDoc;
37 import java.awt.geom.Point2D JavaDoc;
38 import java.util.Random JavaDoc;
39
40 import com.jgoodies.animation.AnimationRenderer;
41
42
43 /**
44  * Paints two colored and often translucent fans that can be rotated.
45  *
46  * @author Karsten Lentzsch
47  * @version $Revision: 1.4 $
48  *
49  * @see com.jgoodies.animation.animations.FanAnimation
50  * @see com.jgoodies.animation.components.FanComponent
51  */

52 public final class FanRenderer implements AnimationRenderer {
53     
54     private static final Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
55     
56     
57     private final Triangle[] triangles;
58     
59     private Point2D JavaDoc origin;
60     private double rotation;
61
62     
63     public FanRenderer(Triangle[] triangles) {
64         this.triangles = triangles;
65     }
66
67
68     public FanRenderer(int triangleCount, Color JavaDoc baseColor) {
69         this(createSectors(triangleCount, baseColor));
70     }
71     
72     
73     public static Triangle[] createSectors(int count, Color JavaDoc baseColor) {
74         Triangle[] result = new Triangle[count];
75         double sectorAngle = Math.PI * 2 / count;
76
77         for (int i = 0; i < count; i++) {
78             double rotation = i * sectorAngle + (random.nextFloat() - 0.5) * Math.PI/10;
79             double angle = sectorAngle * (0.2 + random.nextFloat() * 0.4);
80             result[i] = new Triangle(rotation, angle, nextColor(baseColor));
81         }
82         return result;
83     }
84     
85     private static Color JavaDoc nextColor(Color JavaDoc baseColor) {
86         float[] hsb = new float[3];
87         Color.RGBtoHSB(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), hsb);
88         float brightness = 0.8f + random.nextFloat() * 0.2f;
89         return Color.getHSBColor(hsb[0], hsb[1], brightness);
90     }
91     
92     
93     public Point2D JavaDoc getOrigin() { return origin; }
94     public void setOrigin(Point2D JavaDoc origin) { this.origin = origin; }
95     public double getRotation() { return rotation; }
96     public void setRotation(double rotation) { this.rotation = rotation; }
97     
98     
99     public void render(Graphics2D JavaDoc g2, int width, int height) {
100         double radius = Math.sqrt(width * width + height * height);
101         
102         Point2D JavaDoc p = getOrigin() != null ? getOrigin() : getDefaultOrigin(width, height);
103         
104         g2.translate(p.getX(), p.getY());
105         g2.rotate(rotation);
106         for (int i = 0; i < triangles.length; i++) {
107             triangles[i].render(g2, radius);
108         }
109         g2.rotate(-rotation);
110         g2.translate(-p.getX(), -p.getY());
111     }
112     
113     
114     private Point2D JavaDoc getDefaultOrigin(int width, int height) {
115         return new Point2D.Double JavaDoc(width * 0.75, height * 0.75);
116     }
117     
118
119     // Helper Class ***********************************************************
120

121         
122     // A helper class that models and renders a single sector.
123
private static class Triangle {
124         
125         private final double aRotation;
126         private final double angle;
127         private final Color JavaDoc color;
128         
129         private Triangle(double rotation, double angle, Color JavaDoc color) {
130             this.aRotation = rotation;
131             this.angle = angle;
132             this.color = color;
133         }
134         
135         private static Shape JavaDoc createPolygon(double rotation, double angle, double radius) {
136             double startAngle = rotation - angle / 2;
137             double stopAngle = startAngle + angle;
138             double hypothenusis = radius / Math.cos(angle / 2);
139         
140             float x0 = 0.0f;
141             float y0 = 0.0f;
142             float x1 = (float) (x0 - hypothenusis * Math.cos(startAngle));
143             float y1 = (float) (y0 - hypothenusis * Math.sin(startAngle));
144             float x2 = (float) (x0 - hypothenusis * Math.cos(stopAngle));
145             float y2 = (float) (y0 - hypothenusis * Math.sin(stopAngle));
146             
147             GeneralPath JavaDoc polygon = new GeneralPath JavaDoc(GeneralPath.WIND_EVEN_ODD, 3);
148             polygon.moveTo(x0, y0);
149             polygon.lineTo(x1, y1);
150             polygon.lineTo(x2, y2);
151             polygon.closePath();
152             
153             return polygon;
154         }
155         
156         void render(Graphics2D JavaDoc g2, double radius) {
157             g2.setColor(color);
158             g2.fill(createPolygon(aRotation, angle, radius));
159         }
160     }
161     
162 }
Popular Tags