Skip to main content

Splash Screen with Transparent background OutOfMemoryError

1 reply [Last post]
Joined: 2004-06-29

Hi all,

I have created a non-rectangular splash screen for our application that uses Hack #41 from the book Swing Hacks. 99.9% of the time, it works fine. However, every once in awhile, we get this exception when the splash screen is being launched:

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
at java.lang.reflect.Method.invoke(
at classmcs.launcher.HotPatchableApplication$
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError
at Method)
at java.awt.Robot.createScreenCapture(

The code that we use is this:

public static final class TransparentBackground extends JComponent
* The image being drawn in the background.
private Image background;

* Creates a new Transparent Background.
public TransparentBackground()
setLayout(new BorderLayout());

* Takes a "picture" of what is behind the parent frame and then paints
* it on the transparent parts of the GUI.
public void updateBackground()
// the robot is used to take a snapshot of what is behind this
// GUI.
Robot robot = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dim = tk.getScreenSize();
background = robot.createScreenCapture(
new Rectangle(0,0,(int)dim.getWidth(), (int)dim.getHeight()));
catch (Exception ex)

* Does the actual painting of the "picture".
* @param g The graphics used for painting.
public void paintComponent(Graphics g)
Point pos = this.getLocationOnScreen( );
Point offset = new Point(-pos.x,-pos.y);
g.drawImage(background, offset.x, offset.y, null);

Since this doesn't happen often, it is next to impossible to debug...any thoughts?

Thanks in advance!

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-07-24

(please use [ code ] and [ /code ] tags w/o spaces to post code snippets)

Have you considered using the real SplashScreen that supports transparency and is available since jdk 6?

Also, since 6u10 there's support for native transparent windows.

Otherwise, you'll need to tweak the VM command-line options (like -Xmx, etc.) to give your application more memory.