|
Can't get my thread to sleep!
I'm working on a simple JFrame based app that fades in an image using an alpha filter. The code so far:
import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import static java.lang.Thread.sleep;
public class testAlpha extends JFrame
{
private Image source,result;
private testAlpha()
{
setBounds (0,0,300,325);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
image3screen thescreen = new image3screen();
c.setLayout(new BorderLayout());
c.add(thescreen, BorderLayout.CENTER);
}
public static void main (String args[])
{
testAlpha img1 = new testAlpha();
img1.setVisible(true);
}
}
class image3screen extends JPanel
{
private Image source,result;
private int alphaLevel = 0;
private AlphaFilter af;
public image3screen()
{
Toolkit tkt = Toolkit.getDefaultToolkit();
source = tkt.getImage("rico.jpg");
result = source;
repaint();
alphaLevel = 10;
filterImage();
try
{
sleep(1500);
}
catch (InterruptedException ex) {}
alphaLevel = 25;
filterImage();
try
{
sleep(1500);
}
catch (InterruptedException ex) {}
alphaLevel = 55;
filterImage();
}
private void filterImage()
{
result = createImage(new FilteredImageSource(source.getSource(),new AlphaFilter(alphaLevel)));
repaint();
}
public void paintComponent(Graphics g)
{
g.drawImage(result,0,0,this);
}
}
class AlphaFilter extends RGBImageFilter
{
private int alphaLevel;
public AlphaFilter(int alpha)
{
alphaLevel = alpha;
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb)
{
int alpha = (rgb >> 24) & 0xff;
alpha = (alpha * alphaLevel) / 255;
return ((rgb & 0x00ffffff) | (alpha << 24));
}
}
The main thread goes to sleep, then the application launches. Some help figuring out how to make it repaint, sleep, change Alpha then repaint would be great! Cheers :{p
|