Results 1 to 3 of 3
  1. #1
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    import static java.lang.Thread.*;
    
    
    public class TestAlpha extends JFrame
    {
        private TestAlpha()
        {
            setBounds (0,0,300,325);
            setDefaultCloseOperation(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 implements Runnable
    {
        private Image source, result;
        private int alphaLevel = 0;
        int alphaInc = 10;
        private AlphaFilter af;
        Thread thread;
        boolean filtering = false;
        
        public Image3screen()
        {
            try
            {
                source = ImageIO.read(new File(//"rico.jpg"));
                                               "taiChi.jpg"));
            }
            catch(IOException e)
            {
                 System.out.println("read error: " + e.getMessage());
            }
            result = source;
    //        repaint();  nothing showing yet/nothing to paint
            alphaLevel = alphaInc;
            start();
        }
    
        private void start()
        {
            if(!filtering)
            {
                filtering = true;
                thread = new Thread(this);
                thread.setPriority(NORM_PRIORITY);
                thread.start();
            }
        }
    
        public void run()
        {
            while(filtering)
            {
                try
                {
                    sleep(200);
                }
                catch (InterruptedException ex)
                {
                    System.out.println("interrupted");
                    filtering = false;
                }
    
                alphaLevel += alphaInc;
                if(alphaLevel < 10 || alphaLevel > 90)
                    alphaInc *= -1;
    
                //System.out.println("alphaLevel = " + alphaLevel);
                filterImage();
            }
        } 
    
        private void filterImage() 
        { 
            result = createImage(new FilteredImageSource(source.getSource(),
                         new AlphaFilter(alphaLevel)));
            repaint();
        }
    
        public void paintComponent(Graphics g) 
        {
            super.paintComponent(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));
        } 
    }

  3. #3
    jamesfrize is offline Member
    Join Date
    Mar 2008
    Posts
    19
    Rep Power
    0

    Default Nice one!

    Thanks, the comments were very helpful!
    I'll have to have a closer look at the code and play about with it before I will understand it totally. Eventually I'll put it to some good use and make some graphics for my website. Cheers :{p

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  2. How to use sleep method of the Thread class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 07:42 PM
  3. Replies: 0
    Last Post: 01-28-2008, 08:02 AM
  4. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •