Shake a dialog : Dialog « Swing Components « Java

Shake a dialog


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);

        int SHAKE_DURATION = 1000;
        if (elapsed >= SHAKE_DURATION)

  public void stopShake() {

  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);




Related examples in the same category

1.Vista Transparent DialogVista Transparent Dialog
2.Use this modal dialog to let the user choose one string from a long list
3.Tip Of Day Dialog
4.Swing error dialog (Exception dialog)Swing error dialog (Exception dialog)
5.Swing Login Domain Dialog with animationSwing Login Domain Dialog with animation
6.About dialog
7.Login DialogLogin Dialog
8.The base class for standard dialogs.
9.Password Dialog
10.A popup dialog with a message and a scrollable list of items
11.Custom dialog box to enter dates
12.Dialog Panel
13.Login Panel
14.Exception dump and Dialog Box