Skip to main content

Swing Magic

Posted by javakiddy on July 24, 2006 at 6:26 AM PDT

I suppose if the following was on the Java Posse's podcast it would be referred to as "Pimping my Swing app".

Pimp My Swing App is the Posse's alternative title for the Extreme GUI Makeover sessions at JavaOne — the idea is to play around with AWT, Swing and any other image producing API you can get your mitts on to create strange, wonderful and unique looking user interfaces. And, yes, this past weekend I spent a little time Swing app 'pimping' myself (whatever that actually means!) with some interesting results...Smoke and Mirrors is a functionless WebStart application which demonstrates two pointless but cute UI effects. The first is a smoke effect drifting over the background of some buttons. The second is a reflective glass/mirror effect applied to the contents of a panel. Neither effect is perfect (the glass effect probably shouldn't be applied to sunken textfields, while the buttons need a bright screen for the label to be readable) but they represent only a few hours of hacking and playing about.

Most importantly: both effects are (relatively) cheap!

(WebStart: 70k approx.) — you may want to maximise the window to see the smoke effect fully.

There is a reason why I'm blogging about this, and it's not to fish for "oow that's interesting" comments. I showed the demo to a couple of friends, and they guessed quite quickly how the mirror effect was done, but were stumped by the smoke. Like all good magic tricks, the audience wasted their time devising preposterous explanations, while the real secret was deceptively simple.

Here's a few clues:

  • Mirror uses a subclass of JPanel (although it may be possible to roll it into a PLaF.)
  • The buttons use the Synth PLaF, with a custom SynthPainter for the animated background. They are regular JButton instances.
  • The smoke animation is generated real time. It isn't an animated GIF — that would be cheating.
  • The smoke background is different for each button, yet takes very little to generated. Hint: Synth's drawing of the button border probably takes more graphics operations and number crunching.

The purpose of this blog is to demonstrate that some very interesting graphical effects can be had quite cheaply. Often a bit of slight-of-hand is all that's needed to deceive the user into thinking they are seeing something far more complex and rich than is actually the case. When brightening up your own Swing apps, keep in mind that you may not need to re-create exact Physics or light effects, just spoof them convincingly.

So, without further ado, I throw the challenge open to the audience: without examining the Jar file contents, dare you decipher the riddle that is... [dramatic thunder clap] 'The Secret of the Smoke and Mirrors'...?!?!! :)

Related Topics >>