KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > input > Accelerator


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer.input;
17
18 import java.util.*;
19
20 /**
21  * Class to manage acceleration for a particular object. Acceleration is
22  * provided as a float value and is manipulated on a separate thread.
23  * Acceleration can be initiated by calling the startAcceleration() method and
24  * stopped by calling stopAcceleration() The acceleration can be immediately
25  * reset to normal by calling startAcceleration() twice in succession or by
26  * calling resetAcceleration() directly.
27  *
28  * @author Dusty Phillips [dusty@buchuki.com]
29  */

30 public class Accelerator {
31
32     /**
33      * Start the thread that steadily increases or decreases the acceleration
34      * value If this method is called twice in succession, acceleration returns to
35      * the normal 1.0 value.
36      *
37      * @param accel true for acceleration, false for deceleration
38      */

39     public void startAcceleration(boolean accel) {
40         if (task != null) {
41             resetAcceleration();
42             return;
43         }
44         float accelvalue = accel ? 1.4f : 0.6f;
45         task = new AccelerationTask(accelvalue);
46         scheduler.schedule(task, 1000, 500);
47     }
48
49     /**
50      * Stop the last acceleration event
51      */

52     public void stopAcceleration() {
53         if (task != null) {
54             task.cancel();
55             task = null;
56         }
57     }
58
59     /**
60      * Reset the acceleration to the normal value
61      */

62     public void resetAcceleration() {
63         stopAcceleration();
64         acceleration = 1.0f;
65     }
66
67     /**
68      * Returns the value of acceleration.
69      *
70      * @return The acceleration value
71      */

72     public float getAcceleration() {
73         return acceleration;
74     }
75
76     /**
77      * The current level of acceleration
78      */

79     private float acceleration = 1.0f;
80
81     /**
82      * The timer that the repetitive task is scheduled on
83      */

84     private Timer scheduler = new Timer(true);
85
86     /**
87      * The currently scheduled AccelerationTask, if there is one
88      */

89     private AccelerationTask task;
90
91     /**
92      * Class to periodically increase or decrease the Acceleration value each time
93      * it is executed
94      *
95      * @author Dusty Phillips [dusty@buchuki.com]
96      */

97     private class AccelerationTask extends TimerTask {
98         /**
99          * Constructor an AccelerationTask object with a reference to the increment
100          * value for each execution of the task.
101          *
102          * @param increment the amount to multiply the acceleration value by for
103          * each execution of the task
104          */

105         AccelerationTask(float increment) {
106             this.increment = increment;
107         }
108
109         /**
110          * Perform the routine operations of the TimerTask
111          */

112         public void run() {
113             acceleration *= increment;
114         }
115
116         /**
117          * The current increment for the AccelerationTask
118          */

119         private float increment;
120     }
121 }
122
123
Popular Tags