KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtriver > open > vectorvisuals > task > IndefiniteOscillationTask


1 /*
2  * IndefiniteOscillationTask.java
3  *
4  * Created on 9 July 2003, 15:34
5  */

6
7 package com.thoughtriver.open.vectorvisuals.task;
8
9 import java.awt.geom.*;
10
11 import com.thoughtriver.open.vectorvisuals.*;
12
13 /**
14  * This task can be used to make a <CODE>VisualObject</CODE> rotate back and
15  * forth at a controlled rate indefinitely. Visually, this resembles a winding
16  * then unwinding motion, or the swinging of a pendulum (depending upon the
17  * extent of the oscillation).
18  *
19  * @author Brandon Franklin
20  * @version $Date: 2006/11/25 09:15:42 $
21  */

22 public class IndefiniteOscillationTask extends AnimationTask {
23
24     /** The <CODE>VisualObject</CODE> that will be rotated by this task */
25     private final VisualObject target;
26
27     /** The extent of the oscillation, expressed in Theta */
28     private final double extent;
29
30     /** The rate of oscillation, expressed in Cycles Per Second */
31     private double rate = 0;
32
33     /** The running total used to calculate the change in angle */
34     private double total = 0;
35
36     /** The record of the last known theta position */
37     private double lastKnownPosition = 0;
38
39     /**
40      * Creates a new instance of <CODE>IndefiniteOscillationTask</CODE> with
41      * the specified <CODE>VisualObject</CODE> as its target, and with the
42      * specified extent of oscillation. Initially, the oscillation rate will be
43      * set to 0.
44      *
45      * @param target the <CODE>VisualObject</CODE> that will be rotated by
46      * this task
47      * @param extent the amount back and forth from 0 that the oscillation
48      * should rotate, expressed in Theta
49      */

50     public IndefiniteOscillationTask(final VisualObject target, final double extent) {
51         super(-1);
52         this.target = target;
53         this.extent = extent;
54     }
55
56     /**
57      * Sets the desired rate of oscillation, expressed in Cycles Per Second. The
58      * change will take effect immediately.
59      *
60      * @param rate the desired rate of oscillation, expressed in Cycles Per
61      * Second
62      */

63     public void setRate(final double rate) {
64         this.rate = rate;
65     }
66
67     /**
68      * Causes the task to change its state to reflect an amount of change based
69      * on the supplied number of milliseconds having passed.
70      *
71      * @param progress the amount of progress this <CODE>AnimationTask</CODE>
72      * should be changed to reflect
73      */

74     @Override JavaDoc
75     protected void update(final double progress) {
76
77         synchronized (target) {
78             Point2D rotationPoint = target.getRotationPoint();
79             AffineTransform transform = target.getTransform();
80
81             total += progress;
82             double position = Math.sin((total / (1000 / rate))) * extent;
83             double amountToApply = position - lastKnownPosition;
84             lastKnownPosition = position;
85
86             transform.rotate(amountToApply, rotationPoint.getX(), rotationPoint.getY());
87             target.setTransform(transform);
88         }
89     }
90
91 }
92
Popular Tags