KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * TranslationTask.java
3  *
4  * Created on 11 June 2003, 0:52
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> translate a
15  * specific amount over a set period of time. The effect will be a smooth slide
16  * from the starting location to the destination.
17  *
18  * @author Brandon Franklin
19  * @version $Date: 2006/11/25 09:15:41 $
20  */

21 public class TranslationTask extends AnimationTask {
22
23     /** The <CODE>VisualObject</CODE> that will be translated by this task */
24     private final VisualObject target;
25
26     /** The translation to be executed */
27     private final AffineTransform translate;
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>TranslationTask</CODE> with the
34      * specified <CODE>VisualObject</CODE> as its target, and with the
35      * supplied destination and duration settings.
36      *
37      * @param target the <CODE>VisualObject</CODE> that will be translated by
38      * this task
39      * @param xTranslate the goal X translation
40      * @param yTranslate the goal Y translation
41      * @param duration the number of milliseconds over which the translation
42      * should occur
43      */

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

59     @Override JavaDoc
60     protected void update(final double progress) {
61
62         synchronized (target) {
63             AffineTransform transform = target.getTransform();
64
65             double xToTranslate = (progress - lastProgress)
66                     * translate.getTranslateX();
67             double yToTranslate = (progress - lastProgress)
68                     * translate.getTranslateY();
69             lastProgress = progress;
70
71             transform.translate(xToTranslate, yToTranslate);
72             target.setTransform(transform);
73         }
74     }
75
76 }
77
Popular Tags