SwingX Painters Demo

Richar Bair recently introduced painters in SwingX, something we have been considering for Swing as well. A painter can be seen as a delegate you can plug into a component to change its appearance. As of today, you can call JXPanel.setBackgroundPainter() to customize the background of any instance of JXPanel. SwingX already provides a bunch of painters: image, gradient, shape, matte…

Anyway, I was shopping for MacOS X applications and I stumbled upon a nice looking web site. I showed it to Richard and we immediately tried to think of how we could implement this in Swing. As it turns out, it was extremely easy using SwingX painters and NetBeans 5.0 Matisse GUI Builder. The following picture is the result of this experiment:

Black

Black

In this UI everything is Swing but two elements: the glowing shield and the wooden part. The header containing the title and the shield is drawn using SwingX painters. The following code snippet shows how I implemented this header (the variable called header is an instance of JXPanel):

private void setupPainters() {
    GlossPainter gloss = new GlossPainter();

    PinstripePainter stripes = new PinstripePainter();
    stripes.setPaint(new Color(1.0f, 1.0f, 1.0f, 0.17f));
    stripes.setSpacing(5.0);
    
    MattePainter matte = new MattePainter(new Color(51, 51, 51));

    header.setBackgroundPainter(new CompoundPainter(matte, stripes, gloss));
}

We actually create a compound of three painters: a matte color, some pinstripes and the gloss. You can run the WebStart demo to see it for real or download the NetBeans project. To build and run the project you will need the latest version of SwingX from the CVS. Check out the java.net project page to get access to the CVS.

If you want to play with the painters, I really encourage you to read the tutorial that Richard has just started. You can also generate the javadoc from the source code and look into the package org.jdesktop.swingx.painter.

Enjoy!

3 Responses to “SwingX Painters Demo”

  1. Enterit says:

    Link to Richard’s tutorial does not work.
    I got HTTP 400 error.
    By the way, thanks wor the great demo!