Sunday, April 15, 2007

Make The Swing Fly

Recently this thing struck my head that i can make my JFrame(Swing components)"Fly In" when the application is started and fly out when the quit button is hit and ended up with the following code.Use it and add a better entrance and exit effect to your applications!!!!!
I'll be adding it to NYouTube.

Enjoy!
:-)


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {

JFrame f;
JButton jb;
Dimension t;
public Test(){
f=new JFrame("Flying window");
jb=new JButton("Quit");
//Get the screen size
t=Toolkit.getDefaultToolkit().getScreenSize();
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//The Fly Out effect
for(int i=t.width/2;i>=0;i-=2)
{
f.setLocation(i,i);
f.repaint();

}
System.exit(0);
}
});

f.getContentPane().add(jb);
f.pack();
f.setSize(100,100);
f.setDefaultCloseOperation(3);
f.setVisible(true);
//The Fly In effect
for(int i=0;i<=t.width/2;i+=2)
{
f.setLocation(i,i);
f.repaint();

}

}
public static void main(String[] s)
{
new Test();
}

}

5 comments:

Anonymous said...

Hello,
this is a cool effect, but I'm in doubt, if it will show the same behaviour when I have a really complicated JFrame containing lots of components that have to be redrawn all the time your frame will "fly in or out"...
--Klaus

Ashish Lijhara said...

klaus i have simply changed setLocation() attributes dynamically till the position i want the frame to stop so it will work with the JFrame no matter how complex it may be

Anonymous said...

Sounds good, I will give it a try...
--Klaus

Ashish Lijhara said...

i am sure u'll like the effect

Ashish Lijhara said...

Meh! Seems no parallelism whatsoever!