Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Fade out Swing components

314 views
Skip to first unread message

Andre Rothe

unread,
Jan 26, 2011, 10:02:21 AM1/26/11
to
Hi,

Is it possible to fade out SWING components instead of
setVisible(false)?

Thank you for all ideas,
Andre

John B. Matthews

unread,
Jan 26, 2011, 2:02:36 PM1/26/11
to
In article
<f8a44aaf-3aa9-486d...@w17g2000yqh.googlegroups.com>,
Andre Rothe <aro...@phosco.info> wrote:

> Is it possible to fade out SWING components instead of
> setVisible(false)?

Yes, there are several approaches. This example [1] varies the
background's saturation, while this one [2] changes the alpha of the
graphics context's composite.

[1]<http://stackoverflow.com/questions/2124507>
[2]<http://stackoverflow.com/questions/2234020>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Andre Rothe

unread,
Jan 26, 2011, 5:31:11 PM1/26/11
to
This doesn't work for me. I have a JLabel component and it should
fade out to the background color of the underlying frame.

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int xx = this.getWidth();
int yy = this.getHeight();
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.DST_IN,
model.getAlpha()));
g2d.setColor(getBackground());
g2d.fillRect(0, 0, xx, yy);
}

It doesn't fade, it switches from a black rectangle to the label
content.
"model" stores the current alpha value, which is changed correctly
after 300 ms by 0.1f. After a model change, a repaint() is processed.

~Andre

On Jan 26, 8:02 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <f8a44aaf-3aa9-486d-9a45-e0c93cdda...@w17g2000yqh.googlegroups.com>,

Andre Rothe

unread,
Jan 26, 2011, 5:49:36 PM1/26/11
to
It works! I have set the JLabel as opaque and use
AlphaComposite.SRC_OVER. Nice feature.

Thanks
Andre

Roedy Green

unread,
Jan 28, 2011, 11:12:41 AM1/28/11
to
On Wed, 26 Jan 2011 07:02:21 -0800 (PST), Andre Rothe
<aro...@phosco.info> wrote, quoted or indirectly quoted someone who
said :

>Is it possible to fade out SWING components instead of
>setVisible(false)?

there is setFade( true );

However, you could concoct such a beast by attaching a timer to force
a periodic repaint, and changing the colours on each cycle to
gradually fade them to the background colour.

You could do it by creating "FadingJButton FadingTextArea etc etc. Or
just have a Fader that you feed a set of components to that it calls
setForeground, setBackground and repaint on with a SwingTimer.
It would remember the original colours and the current colours so it
it bring them all back to life. Remember if you setVisible(false)
then validate, the screen real estate will be recycled for other
components. With your fader, that would not happen till the last
minute when you did a setVisible(false).
--
Roedy Green Canadian Mind Products
http://mindprod.com
To err is human, but to really foul things up requires a computer.
~ Farmer's Almanac
It is breathtaking how a misplaced comma in a computer program can
shred megabytes of data in seconds.

Ian Shef

unread,
Jan 31, 2011, 2:29:38 PM1/31/11
to
Roedy Green <see_w...@mindprod.com.invalid> wrote in
news:feq5k6pq6t7v2kadf...@4ax.com:

> On Wed, 26 Jan 2011 07:02:21 -0800 (PST), Andre Rothe
> <aro...@phosco.info> wrote, quoted or indirectly quoted someone who
> said :
>
>>Is it possible to fade out SWING components instead of
>>setVisible(false)?
>
> there is setFade( true );

<snip>

OK, I'll take the bait...

In what version of Java, in what object, is there a setFade method ?

I can't find it in version 6 Javadocs.

0 new messages