3D splash screen with Swing

You can run the WebStart demo or, as usual, download its source code.

I recently showed you how to create a custom splash screen casting a shadow on the desktop. I also showed you, in one of my first demos, how to tightly integrate Swing and Java3D. Hence, I decided to combine these tricks to create an astounding 3D animated splash screen:

Now here is the source code of this cool new component:

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;

public class SplashScreen3D extends JWindow {
    public SplashScreen3D(BranchGroup scene, int width, int height) {
        setSize(new Dimension(width, height));
        createCanvas3D(scene);
    }

    protected void createCanvas3D(BranchGroup scene) {
        Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        canvas3D.setSize(getWidth(), getHeight());

        BufferedImage backgroundImage = createBackground();
        if (backgroundImage != null) {
	        Background background = new Background(
                  new ImageComponent2D(ImageComponent2D.FORMAT_RGB,
                                       backgroundImage));
	        BoundingSphere bounds = new BoundingSphere();
	        bounds.setRadius(100.0);
	        background.setApplicationBounds(bounds);
	        scene.addChild(background);
        }

        SimpleUniverse universe = new SimpleUniverse(canvas3D);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(scene);
    
        View view = universe.getViewer().getView();
        view.setSceneAntialiasingEnable(true);

        getContentPane().add(canvas3D);
    }

    protected BufferedImage createBackground() {
        setLocationRelativeTo(null);
        Rectangle windowRect = getBounds();

        try {
            Robot robot = new Robot(getGraphicsConfiguration().getDevice());
            BufferedImage capture = robot.createScreenCapture(
                                      new Rectangle(windowRect.x,
                                                    windowRect.y,
                                                    windowRect.width,
                                                    windowRect.height));
            return capture;
        } catch (AWTException e) { }

        return null;
    }
}

As you can see, the code is very simple. When the splash screen is created, a capture of the underlying desktop is performed with the help of the Robot class. The resulting image is then used as a background in the Java3D scene. You can download the source code to see how I created the Java3D scene graph.

One Response to “3D splash screen with Swing”

  1. This is what I need, thank you!!!