KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > components > FanComponent


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.components;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Graphics JavaDoc;
35 import java.awt.Graphics2D JavaDoc;
36 import java.awt.Insets JavaDoc;
37 import java.awt.RenderingHints JavaDoc;
38 import java.awt.geom.Point2D JavaDoc;
39
40 import javax.swing.JComponent JavaDoc;
41
42 import com.jgoodies.animation.renderer.FanRenderer;
43
44 /**
45  * A Swing component that paints a set of triangles as a fan.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.4 $
49  *
50  * @see com.jgoodies.animation.animations.FanAnimation
51  * @see com.jgoodies.animation.renderer.FanRenderer
52  */

53 public final class FanComponent extends JComponent JavaDoc {
54
55     private final FanRenderer renderer;
56     
57     // Instance Creation ****************************************************
58

59     /**
60      * Constructs a <code>FanComponent</code> for the specified
61      * number of triangles and base color.
62      *
63      * @param triangleCount the number of triangles to be displayed
64      * @param baseColor the base color used to build the translucent
65      * triangle colors from
66      */

67     public FanComponent(int triangleCount, Color JavaDoc baseColor) {
68         this.renderer = new FanRenderer(triangleCount, baseColor);
69     }
70
71     // Accessors ************************************************************
72

73     public Point2D JavaDoc getOrigin() {
74         return renderer.getOrigin();
75     }
76     
77     public double getRotation() {
78         return renderer.getRotation();
79     }
80
81     /**
82      * Sets a new origin of the fan.
83      *
84      * @param origin the origin to be set
85      */

86     public void setOrigin(Point2D JavaDoc origin) {
87         renderer.setOrigin(origin);
88         repaint();
89     }
90
91     /**
92      * Sets a new rotation.
93      *
94      * @param rotation the rotation to be set
95      */

96     public void setRotation(double rotation) {
97         renderer.setRotation(rotation);
98         repaint();
99     }
100
101     /**
102      * Delegates painting to the fan renderer. Switches on anti-aliasing
103      * and the high quality mode before invoking the renderer.
104      *
105      * @param g the Graphics object to render on
106      */

107     public void paintComponent(Graphics JavaDoc g) {
108         Graphics2D JavaDoc g2 = (Graphics2D JavaDoc) g;
109
110         g2.setRenderingHint(
111             RenderingHints.KEY_ANTIALIASING,
112             RenderingHints.VALUE_ANTIALIAS_ON);
113         g2.setRenderingHint(
114             RenderingHints.KEY_RENDERING,
115             RenderingHints.VALUE_RENDER_QUALITY);
116
117         Insets JavaDoc insets = getInsets();
118         int x = insets.left;
119         int y = insets.top;
120         int w = getWidth() - x - insets.right;
121         int h = getHeight() - y - insets.bottom;
122         g2.translate(x, y);
123         renderer.render(g2, w, h);
124         g2.translate(-x, -y);
125     }
126
127 }
Popular Tags