001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.animation.components;
032:
033: import java.awt.Color;
034: import java.awt.Graphics;
035: import java.awt.Graphics2D;
036: import java.awt.Insets;
037: import java.awt.RenderingHints;
038: import java.awt.geom.Point2D;
039:
040: import javax.swing.JComponent;
041:
042: import com.jgoodies.animation.renderer.FanRenderer;
043:
044: /**
045: * A Swing component that paints a set of triangles as a fan.
046: *
047: * @author Karsten Lentzsch
048: * @version $Revision: 1.1 $
049: *
050: * @see com.jgoodies.animation.animations.FanAnimation
051: * @see com.jgoodies.animation.renderer.FanRenderer
052: */
053: public final class FanComponent extends JComponent {
054:
055: private final FanRenderer renderer;
056:
057: // Instance Creation ****************************************************
058:
059: /**
060: * Constructs a <code>FanComponent</code> for the specified
061: * number of triangles and base color.
062: *
063: * @param triangleCount the number of triangles to be displayed
064: * @param baseColor the base color used to build the translucent
065: * triangle colors from
066: */
067: public FanComponent(int triangleCount, Color baseColor) {
068: this .renderer = new FanRenderer(triangleCount, baseColor);
069: }
070:
071: // Accessors ************************************************************
072:
073: public Point2D getOrigin() {
074: return renderer.getOrigin();
075: }
076:
077: public double getRotation() {
078: return renderer.getRotation();
079: }
080:
081: /**
082: * Sets a new origin of the fan.
083: *
084: * @param origin the origin to be set
085: */
086: public void setOrigin(Point2D origin) {
087: renderer.setOrigin(origin);
088: repaint();
089: }
090:
091: /**
092: * Sets a new rotation.
093: *
094: * @param rotation the rotation to be set
095: */
096: public void setRotation(double rotation) {
097: renderer.setRotation(rotation);
098: repaint();
099: }
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 g) {
108: Graphics2D g2 = (Graphics2D) g;
109:
110: g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
111: RenderingHints.VALUE_ANTIALIAS_ON);
112: g2.setRenderingHint(RenderingHints.KEY_RENDERING,
113: RenderingHints.VALUE_RENDER_QUALITY);
114:
115: Insets insets = getInsets();
116: int x = insets.left;
117: int y = insets.top;
118: int w = getWidth() - x - insets.right;
119: int h = getHeight() - y - insets.bottom;
120: g2.translate(x, y);
121: renderer.render(g2, w, h);
122: g2.translate(-x, -y);
123: }
124:
125: }
|