001: /*
002: * Copyright (c) 2005, romain guy (romain.guy@jext.org) and craig wickesser (craig@codecraig.com)
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
006: *
007: * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
008: * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
009: * * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
010: *
011: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
012: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
013: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
015: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
016: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
017: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
018: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
019: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
020: * POSSIBILITY OF SUCH DAMAGE.
021: */
022:
023: package net.java.swingfx.rubberband;
024:
025: import java.awt.BasicStroke;
026: import java.awt.Stroke;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029:
030: import javax.swing.Timer;
031:
032: import net.java.swingfx.rubberband.canvas.RubberBandCanvas;
033: import net.java.swingfx.rubberband.rubberbands.RubberBand;
034:
035: /**
036: * Handles the necessary work of animating the stroke of a {@link RubberBand}.
037: *
038: * The idea for having an animated rubber band came from the java.net project,
039: * <a href="https://rubber-band.dev.java.net/">Rubber-Band</a>
040: *
041: * The code was extracted from the project and modified to work with this
042: * Rubber Band API.
043: *
044: * @author rwickesser
045: * @since 1.0
046: * $Revision: 1.1 $
047: */
048: public class AnimatedStroke {
049: private static final int DASH_WIDTH = 3;
050: private static final int STROKE_COUNT = 2 * DASH_WIDTH;
051: private static final int DASH_SPEED = 50;
052:
053: private int strokeIndex;
054: private final Stroke[] strokes;
055: private final Timer animationTimer;
056:
057: /** the canvas which the rubber band will be drawn on */
058: private RubberBandCanvas canvas;
059:
060: public AnimatedStroke(RubberBandCanvas canvas) {
061: this .canvas = canvas;
062: animationTimer = new Timer(DASH_SPEED,
063: getAnimationActionListener());
064: strokes = new Stroke[STROKE_COUNT];
065: initStrokes();
066: }
067:
068: /**
069: * Initializes the array of <code>Stroke</code>'s
070: */
071: private void initStrokes() {
072: float[] dash = { DASH_WIDTH, DASH_WIDTH };
073: for (int i = 0; i < STROKE_COUNT; i++) {
074: strokes[i] = new BasicStroke(1, BasicStroke.CAP_BUTT,
075: BasicStroke.JOIN_MITER, 1, dash, i);
076: }
077: }
078:
079: /**
080: * Returns the <code>Stroke</code> at <code>strokeIndex</code>
081: *
082: * @return the <code>Stroke</code> at <code>strokeIndex</code>
083: */
084: public Stroke getStroke() {
085: return strokes[strokeIndex];
086: }
087:
088: /**
089: * Starts the animated stroke
090: */
091: public void startAnimation() {
092: animationTimer.start();
093: }
094:
095: /**
096: * Stops the animated stroke
097: */
098: public void stopAnimation() {
099: animationTimer.stop();
100: }
101:
102: /**
103: * Returns a new <code>ActionListener</code> that updates the stroke
104: * as needed
105: *
106: * @return a new <code>ActionListener</code> that updates the stroke
107: * as needed
108: */
109: private ActionListener getAnimationActionListener() {
110: return new ActionListener() {
111: public void actionPerformed(ActionEvent e) {
112: strokeIndex++;
113: strokeIndex %= STROKE_COUNT;
114: canvas.getCanvas().repaint();
115: }
116: };
117: }
118: }
|