KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RotationTask.java
3  *
4  * Created on 9 June 2003, 14:21
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 a specific
15  * amount over a set period of time. This is appropriate for things like
16  * rotating tank turrets or changing the direction of arrows.
17  *
18  * @author Brandon Franklin
19  * @version $Date: 2006/11/25 09:15:42 $
20  */

21 public class RotationTask extends AnimationTask {
22
23     /** The <CODE>VisualObject</CODE> that will be rotated by this task */
24     private VisualObject target = null;
25
26     /** The angle to rotate, expressed in Theta */
27     private double angle = 0;
28
29     /** The last recorded amount of progress made by this task */
30     private double lastProgress = 0;
31
32     /**
33      * Creates a new instance of <CODE>RotationTask</CODE> with the specified
34      * <CODE>VisualObject</CODE> as its target, and with the supplied angle
35      * and duration settings.
36      *
37      * @param target the <CODE>VisualObject</CODE> that will be rotated by
38      * this task
39      * @param angle the angle to rotate the target
40      * @param duration the number of milliseconds over which the rotation should
41      * occur
42      */

43     public RotationTask(final VisualObject target, final double angle, final int duration) {
44         super(duration);
45         this.target = target;
46         this.angle = angle;
47         lastProgress = 0;
48     }
49
50     /**
51      * Causes the task to change its state to reflect the amount of progress
52      * reported. The parameter is a double with valid values between 0.0 and
53      * 1.0.
54      *
55      * @param progress the amount of progress this <CODE>AnimationTask</CODE>
56      * should be changed to reflect
57      */

58     @Override JavaDoc
59     protected void update(final double progress) {
60
61         synchronized (target) {
62             Point2D rotationPoint = target.getRotationPoint();
63             AffineTransform transform = target.getTransform();
64
65             double thetaToRotate = (progress - lastProgress) * angle;
66             lastProgress = progress;
67
68             transform.rotate(thetaToRotate, rotationPoint.getX(), rotationPoint.getY());
69             target.setTransform(transform);
70         }
71     }
72
73 }
74
Popular Tags