KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ScalingTask.java
3  *
4  * Created on 8 July 2003, 23:02
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> scale to a
15  * specific size over a set period of time. The effect will be a smooth
16  * rescaling from the starting size to the target size.
17  *
18  * @author Brandon Franklin
19  * @version $Date: 2006/11/25 09:15:42 $
20  */

21 public class ScalingTask extends AnimationTask {
22
23     /** The <CODE>VisualObject</CODE> that will be scaled by this task */
24     private final VisualObject target;
25
26     /** The target scale to be achieved */
27     private final AffineTransform endScale;
28
29     /** The X scale delta to reach the target */
30     private double xDelta = 0.0;
31
32     /** The Y scale delta to reach the target */
33     private double yDelta = 0.0;
34
35     /**
36      * Creates a new instance of <CODE>ScalingTask</CODE> with the specified
37      * <CODE>VisualObject</CODE> as its target, and with the supplied goal and
38      * duration settings.
39      *
40      * @param target the <CODE>VisualObject</CODE> that will be scaled by this
41      * task
42      * @param xScale the goal X scale
43      * @param yScale the goal Y scale
44      * @param duration the number of milliseconds over which the scale should
45      * occur
46      */

47     public ScalingTask(final VisualObject target, final double xScale, final double yScale, final int duration) {
48         super(duration);
49         this.target = target;
50         this.endScale = AffineTransform.getScaleInstance(xScale, yScale);
51     }
52
53     /**
54      * {@inheritDoc}
55      */

56     @Override JavaDoc
57     protected void init() {
58         AffineTransform transform = target.getTransform();
59         xDelta = endScale.getScaleX() - transform.getScaleX();
60         yDelta = endScale.getScaleY() - transform.getScaleY();
61     }
62
63     /**
64      * Causes the task to change its state to reflect the amount of progress
65      * reported. The parameter is a double with valid values between 0.0 and
66      * 1.0.
67      *
68      * @param progress the amount of progress this <CODE>AnimationTask</CODE>
69      * should be changed to reflect
70      */

71     @Override JavaDoc
72     protected void update(final double progress) {
73
74         synchronized (target) {
75             AffineTransform transform = target.getTransform();
76             double[] matrix = new double[6];
77             transform.getMatrix(matrix);
78
79             double xScale = progress * xDelta;
80             double yScale = progress * yDelta;
81             matrix[0] = endScale.getScaleX() - (xDelta - xScale);
82             matrix[3] = endScale.getScaleY() - (yDelta - yScale);
83
84             transform = new AffineTransform(matrix);
85             target.setTransform(transform);
86         }
87     }
88
89 }
90
Popular Tags