This discussion is archived
3 Replies Latest reply: Oct 9, 2004 4:29 AM by 807587 RSS

Make images fade in & out?

807587 Newbie
Currently Being Moderated
Hi, we're redeveloping our application in swing (java 1.4.2).
When the application is not in use it displays several images on the screen. We want these to smoothly fade in and out (not necessarily synchronised with each other) so as to be more visually appealing.

Any recommendations on a good technique?
We don't want to use animated gifs due to the 256-colour limit - is there a way to do this that isn't CPU-intensive? Something we can use with PNGs, for example?

TIA
Phil
  • 1. Re: Make images fade in & out?
    807587 Newbie
    Currently Being Moderated
    well perhaps you can override the paint method, which will change the opacity of the image...and maybe use some type of thread or timer to animate that fade in/out affect. Set change the opacity used by the Graphics2D object in a paint method do the following:
        /*
         * Set alpha composite.  For example, pass in 1.0f to have 100% opacity
         *  pass in 0.25f to have 25% opacity.
         */
        private AlphaComposite makeComposite(float alpha) {
            int type = AlphaComposite.SRC_OVER;
            return (AlphaComposite.getInstance(type, alpha));
        }
    
         public void paint(Graphics g) {
                  Graphics2D g2d = (Graphics2D) g;
                  g2d.setComposite(makeComposite(.25f));
                  // do some painting...
          }
    that should give you some idea.

    good luck!
  • 2. Re: Make images fade in & out?
    807587 Newbie
    Currently Being Moderated
    Excellent thanks, I'll give it a try soon...
  • 3. Re: Make images fade in & out?
    807587 Newbie
    Currently Being Moderated
    yep that did the trick, and it's smooth. Thanks