KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > key > passwd > swing > HurlingWindowFeedback


1 /*
2  * Copyright 2006 Schlichtherle IT Services
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
17 package de.schlichtherle.key.passwd.swing;
18
19 import java.awt.Point JavaDoc;
20 import java.awt.Window JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.Timer JavaDoc;
26
27 /**
28  * Provides feedback by beeping using the default toolkit, disabling the
29  * default button in the root pane for three seconds and concurrently
30  * hurling the containing window for 1.5 seconds.
31  * <p>
32  * This class is inspired by chapter #38 "Earthquake Dialog" of the book
33  * "Swing Hacks" by Joshua Marinacci & Chris Adamson, published by O'Reilly
34  * in 2005.
35  *
36  * @author Christian Schlichtherle
37  * @version @version@
38  * @since TrueZIP 6.4
39  */

40 public class HurlingWindowFeedback extends BasicInvalidKeyFeedback {
41
42     private static final double PI = Math.PI;
43     private static final double TWO_PI = 2.0 * PI;
44     
45     public static final int DISTANCE = 25;
46     public static final int CYCLE = 150;
47     public static final int DURATION = 1500;
48     public static final int FPS = 75;
49
50     private final double distance;
51     private final double cycle;
52     private final int duration;
53     private final int fps;
54     
55     public HurlingWindowFeedback() {
56         this(DISTANCE, CYCLE, DURATION, FPS);
57     }
58
59     /**
60      * Constructs a new <code>HurlingWindowFeedback</code>.
61      *
62      * @param distance The maximum distance for quaking the window.
63      * @param cycle Milliseconds required for one cycle.
64      * @param duration Millisecons of duration of quake.
65      * @param fps Frames per second for animation.
66      */

67     protected HurlingWindowFeedback(
68             final int distance,
69             final int cycle,
70             final int duration,
71             final int fps) {
72         this.distance = distance;
73         this.cycle = cycle;
74         this.duration = duration;
75         this.fps = fps;
76
77         if (duration > getDelay())
78             setDelay(duration);
79     }
80
81     protected void startAnimation() {
82         super.startAnimation(); // temporarily disable default button
83

84         final Window JavaDoc window = SwingUtilities.getWindowAncestor(getPanel());
85         if (window == null)
86             return;
87
88         final Point JavaDoc origin = window.getLocation();
89         final long startTime = System.currentTimeMillis();
90         final Timer JavaDoc timer = new Timer JavaDoc(1000 / fps, new ActionListener JavaDoc() {
91             public void actionPerformed(ActionEvent JavaDoc e) {
92                 // Calculate elapsed time.
93
final long elapsed = System.currentTimeMillis() - startTime;
94
95                 if (elapsed < duration && window.isShowing()) {
96                     final double amplitude
97                             = Math.sin(PI * elapsed / duration) * distance;
98                     final double angle = TWO_PI * elapsed / cycle;
99                     final int quakingX
100                             = (int) (Math.cos(angle) * amplitude + origin.x);
101                     final int quakingY
102                             = (int) (Math.sin(angle) * amplitude + origin.y);
103
104                     window.setLocation(quakingX, quakingY);
105                     window.repaint();
106                 } else {
107                     ((Timer JavaDoc) e.getSource()).stop();
108                     window.setLocation(origin);
109                     window.repaint();
110                 }
111             }
112         });
113         timer.start();
114     }
115 }
116
Popular Tags