1 16 17 package de.schlichtherle.key.passwd.swing; 18 19 import java.awt.Point ; 20 import java.awt.Window ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 24 import javax.swing.SwingUtilities ; 25 import javax.swing.Timer ; 26 27 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 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(); 84 final Window window = SwingUtilities.getWindowAncestor(getPanel()); 85 if (window == null) 86 return; 87 88 final Point origin = window.getLocation(); 89 final long startTime = System.currentTimeMillis(); 90 final Timer timer = new Timer (1000 / fps, new ActionListener () { 91 public void actionPerformed(ActionEvent e) { 92 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 ) e.getSource()).stop(); 108 window.setLocation(origin); 109 window.repaint(); 110 } 111 } 112 }); 113 timer.start(); 114 } 115 } 116 | Popular Tags |