Java Swing - Vibrating JDialog
Now Let's doing some fun with Java Swing,
Here we will create one JDialog. And when we will run it, it will vibrate or shake.
Want to know how can it be done....then stick with me friends.
Output will be:
1. Normal JDialog 2. Vibrantingg JDialogggg
It's simple as Java Swing provides lots of customization to its components as we need.
So here we do nothing but customise the JDialog code with adding a bit of animation cycles with the help of Timer class.
Just copy the below code of VibrantDialog.java and paste it in your Java Editor and run it...
-----------------------------------------------------------------------------
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class VibrantDialog extends Object {
public static final int SHAKE_DISTANCE = 10;
public static final double SHAKE_CYCLE = 50;
public static final int SHAKE_DURATION = 1000;
public static final int SHAKE_UPDATE = 5;
private JDialog dialog;
private Point naturalLocation;
private long startTime;
private Timer shakeTimer;
private final double TWO_PI = Math.PI * 2.0;
public VibrantDialog (JDialog d) {
dialog = d;
}
public void startShake() {
naturalLocation = dialog.getLocation();
startTime = System.currentTimeMillis();
shakeTimer = new Timer(SHAKE_UPDATE,
new ActionListener( ) {
@Override
public void actionPerformed (ActionEvent e) {
// calculate elapsed time
long elapsed = System.currentTimeMillis()-
startTime;
// use sin to calculate an x-offset
double waveOffset = (elapsed % SHAKE_CYCLE) /
SHAKE_CYCLE;
double angle = waveOffset * TWO_PI;
// offset the x-location by an amount
// proportional to the sine, up to
// shake_distance
int shakenX = (int) ((Math.sin (angle) *
SHAKE_DISTANCE) +
naturalLocation.x);
dialog.setLocation (shakenX, naturalLocation.y);
dialog.repaint( );
// should we stop timer?
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 ("You've totally screwed up your login\n" +
"Go back and do it again… and do you think\n" +
"you could remember your password this time?",
JOptionPane.ERROR_MESSAGE, JOptionPane.OK_OPTION);
JDialog d = pane.createDialog (null, "Shakin'!");
VibrantDialog dec = new VibrantDialog (d);
d.pack( );
d.setModal (false);
d.setVisible(true);
dec.startShake( );
// wait (forever) for a non-null click and then quit
while (pane.getValue( ) == JOptionPane.UNINITIALIZED_VALUE ) {
try { Thread.sleep(100); }
catch (InterruptedException ie) {}
}
System.exit(0);
}
}
-----------------------------------------------------------------------------
Here we are using Timer class constructor.
In Timer Constructor we pass 2 parameters.
1) Shake Update Time: This is the shaking updating time in milli seconds. After what millis, the
vibrating happens..
2) Action Listener() : The second parameter is the action listener we have created for
Vibrating action happens.
All the magic works here. Here we are doing some maths for
calculating the x and y positions of JDialog at the time of shaking. And
when time over the shaking is stopped by stoping the timer.
Where To Use:
This is very useful in login based application. When user provide wrong username or password, instead of notify error through JOptionPane use this VibrantDialog.
Also useful when user do some serious error and you want to notify him/her for that error.
So, this is all the fun with Vibrant JDialog.....
I hope you enjoyed this new trick,...!
Always inviting your comments.....! Thank you.
Yours Friend,
Nirav Raval
----------------------------------------------------------------------------------
Follow Other Java Swing Tricks....
No comments:
Post a Comment
Leave Comment If You Like This...Thank You..!