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

    Default Alpha Fade Flicker

    I'm tryin' to make an applet that displays a slowly fading image against a white background. It works ish, it's just a bit pants! The background is all grey and it flickers like a bugger. I'm used to using the Toolkit which automatically handles all the Image stuff for me.
    I've added an HTML file to run the main class, Unfortunately the images were 2 big to upload here.

    Java Code:
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import static java.lang.Thread.*;
    import java.applet.*; 
    import java.awt.event.*;
    import java.net.*;
    
    public class index extends JApplet 
    {
    String file = "opened.wav";
    Image source;
    MediaTracker mt;
    
        public void init()
        {
            mt = new MediaTracker(this);
            source = getImage(getCodeBase(), "rico.jpg");
            mt.addImage(source,1);
        
            try
            {
                mt.waitForAll();      
            }
                catch(Exception e)
            {
                System.out.println("read error: " + e.getMessage());
            }
                
            setBounds (0,0,300,325);
            Container c = getContentPane();
            Image3screen thescreen = new Image3screen(source);
            c.setLayout(new BorderLayout());
            c.add(thescreen, BorderLayout.CENTER);
            
             addMouseListener
              (
                  new MouseAdapter()
                  {
                  public void mousePressed(MouseEvent me)
                  {
                  AudioClip ac = getAudioClip(getCodeBase(), file);
                  ac.play();
    
                          try
                          { 
                          AppletContext a = getAppletContext();                    
                          URL u = new URL("http://jamesfrize.43host.info/sonodrome.html");
                          a.showDocument(u,"_self");
                          }
                          catch (Exception e) 
                          {
                          System.out.println("read error: " + e.getMessage());
                          }
                  }
                  }
              );
        }
    }
    
    class Image3screen extends JPanel implements Runnable
    {
        int alphaLevel = 0;
        int alphaInc = 5;
        AlphaFilter af;
        Thread thread;
        boolean filtering = false;
        Image source, result;
        
        public Image3screen(Image sourceImage)
        {     
            source = sourceImage;
            result = source;
            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(50);
                }
                catch (InterruptedException ex)
                {
                    System.out.println("interrupted");
                    filtering = false;
                }
    
                alphaLevel += alphaInc;
                if(alphaLevel < 5 || alphaLevel > 250)
                alphaInc *= -1;
    
                filterImage();
                repaint();
            }
        } 
    
        private void filterImage() 
        { 
            result = createImage(new FilteredImageSource(source.getSource(),
            new AlphaFilter(alphaLevel)));
        }
    
        public void paintComponent(Graphics g) 
        {
            g.setColor(Color.white);
            g.fillRect(0,0,300,300);
            g.drawImage(result,0,0,this);
        } 
    }
    
    class AlphaFilter extends RGBImageFilter 
    {
        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));
        } 
    }
    Cheers, :{p
    Attached Files Attached Files

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

    Default

    Anyone have any clues?? I'm used to Java automatically handling the Image Buffering, I use the swing toolkit. I've not as yet had to double buffer anything. Is this one of the cases where it would be needed?

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

    Default

    Java Code:
    //  <applet code="FadeTest" width="300" height="300"></applet>
    
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;
    import static java.lang.Thread.*;
    import java.applet.*; 
    import java.awt.event.*;
    import java.net.*;
    
    public class FadeTest extends JApplet 
    {
        String file = "opened.wav";
        Image source;
        MediaTracker mt;
    
        public void init()
        {
            mt = new MediaTracker(this);
            source = getImage(getCodeBase(), "rico.jpg");
                                             //"images/blackBear.jpg");
            mt.addImage(source,1);
        
            try
            {
                mt.waitForAll();      
            }
                catch(Exception e)
            {
                System.out.println("read error: " + e.getMessage());
            }
                
            setBounds (0,0,300,325);
            Container c = getContentPane();
            ImageScreen2 thescreen = new ImageScreen2(source);
            c.setLayout(new BorderLayout());
            c.add(thescreen, BorderLayout.CENTER);
            
            addMouseListener(new MouseAdapter()
            {
                public void mousePressed(MouseEvent me)
                {
                    AudioClip ac = getAudioClip(getCodeBase(), file);
                    ac.play();
    
                    try
                    { 
                        AppletContext a = getAppletContext();                    
                        URL u = new URL("http://jamesfrize.43host.info/" +
                                        "sonodrome.html");
                        a.showDocument(u,"_self");
                    }
                    catch (Exception e) 
                    {
                        System.out.println("read error: " + e.getMessage());
                    }
                }
            });
        }
    }
    
    class ImageScreen2 extends JPanel implements Runnable
    {
        Thread thread;
        boolean filtering = false;
        Image source;
        int rule = AlphaComposite.SRC_OVER;
        float alpha = 0;
        float alphaInc = 0.01f;
        
        public ImageScreen2(Image sourceImage)
        {     
            source = sourceImage;
            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(100);
                }
                catch (InterruptedException ex)
                {
                    System.out.println("interrupted");
                    filtering = false;
                }
    
                alpha += alphaInc;
                if(alpha < 0.01f || alpha > 0.99f)
                alphaInc *= -1;
    
                repaint();
            }
        }
    
        protected void paintComponent(Graphics g) 
        {
    //        g.setColor(Color.white);
    //        g.fillRect(0,0,300,300);
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            Composite orig = g2.getComposite();
            AlphaComposite ac = AlphaComposite.getInstance(rule, alpha);
            g2.setComposite(ac);
            g2.drawImage(source,0,0,this);
            g2.setComposite(orig);
        } 
    }

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

Similar Threads

  1. flicker
    By angel_eyez in forum New To Java
    Replies: 3
    Last Post: 01-14-2008, 10:52 PM
  2. Flicker flicker!
    By angel_eyez in forum Java 2D
    Replies: 1
    Last Post: 01-13-2008, 07:52 AM
  3. MegaBlock 0.0.1 alpha
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 08:25 PM
  4. SableCC 4-alpha.2
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-05-2007, 12:16 PM
  5. SableCC 4-alpha.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-23-2007, 07:24 PM

Posting Permissions

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