Java Frame with Fading Effect
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
import java.awt.*;
public class FadeDemo extends JFrame
{
JFrame frm=null;
JLabel l1=new JLabel("Look at Frame, Its Fading",JLabel.CENTER);
float s=1.0f;
boolean flag=true;
FadeDemo()
{ frm=this;
frm.setUndecorated(true);
frm.getContentPane().setBackground(Color.orange);
l1.setFont(new Font("Times new roman",Font.BOLD,12));
add(l1,BorderLayout.CENTER);
setBounds(100,50,450,550);
setVisible(true);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{ public void run()
{
if(s<=0.9f && flag)
{
frm.setOpacity(s);
s+=0.01f;
}
else
{
frm.setOpacity(s);
s-=0.01f;
if(s<=0.01f)
flag=true;
else
flag=false;
}
} //run
},0,10);
}
public static void main(String[] args)
{
new FadeDemo();
}
}
Output of Program :
import java.util.TimerTask;
import javax.swing.*;
import java.awt.*;
public class FadeDemo extends JFrame
{
JFrame frm=null;
JLabel l1=new JLabel("Look at Frame, Its Fading",JLabel.CENTER);
float s=1.0f;
boolean flag=true;
FadeDemo()
{ frm=this;
frm.setUndecorated(true);
frm.getContentPane().setBackground(Color.orange);
l1.setFont(new Font("Times new roman",Font.BOLD,12));
add(l1,BorderLayout.CENTER);
setBounds(100,50,450,550);
setVisible(true);
Timer timer = new Timer();
timer.schedule(new TimerTask()
{ public void run()
{
if(s<=0.9f && flag)
{
frm.setOpacity(s);
s+=0.01f;
}
else
{
frm.setOpacity(s);
s-=0.01f;
if(s<=0.01f)
flag=true;
else
flag=false;
}
} //run
},0,10);
}
public static void main(String[] args)
{
new FadeDemo();
}
}
Output of Program :
Comments
Post a Comment