import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class Main {
JDialog dialog;
Point naturalLocation;
Timer shakeTimer;
public Main(JDialog d) {
dialog = d;
}
public void startShake() {
final long startTime;
naturalLocation = dialog.getLocation();
startTime = System.currentTimeMillis();
shakeTimer = new Timer(5, new ActionListener() {
public void actionPerformed(ActionEvent e) {
double TWO_PI = Math.PI * 2.0;
double SHAKE_CYCLE = 50;
long elapsed = System.currentTimeMillis() - startTime;
double waveOffset = (elapsed % SHAKE_CYCLE) / SHAKE_CYCLE;
double angle = waveOffset * TWO_PI;
int SHAKE_DISTANCE = 10;
int shakenX = (int) ((Math.sin(angle) * SHAKE_DISTANCE) + naturalLocation.x);
dialog.setLocation(shakenX, naturalLocation.y);
dialog.repaint();
int SHAKE_DURATION = 1000;
if (elapsed >= SHAKE_DURATION)
stopShake();
}
});
shakeTimer.start();
}
public void stopShake() {
shakeTimer.stop();
dialog.setLocation(naturalLocation);
dialog.repaint();
}
public static void main(String[] args) {
JOptionPane pane = new JOptionPane("your message",JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);
JDialog d = pane.createDialog(null, "title");
Main dec = new Main(d);
d.pack();
d.setModal(false);
d.setVisible(true);
dec.startShake();
}
}
|