Skip to main content

Splash Screen with Transparent background OutOfMemoryError

1 reply [Last post]
howlerzz
Offline
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:

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at classmcs.launcher.HotPatchableApplication$2.run(HotPatchableApplicati
on.java:789)
Caused by: java.lang.OutOfMemoryError: OutOfMemoryError
at sun.awt.windows.WRobotPeer.getRGBPixels(Native Method)
at sun.awt.windows.WRobotPeer.getRGBPixels(WRobotPeer.java:47)
at java.awt.Robot.createScreenCapture(Robot.java:329)

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());
updateBackground();
}

/**
* Takes a "picture" of what is behind the parent frame and then paints
* it on the transparent parts of the GUI.
*/
public void updateBackground()
{
try
{
// 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)
{
System.out.println(ex.toString());
ex.printStackTrace();
}
}

/**
* Does the actual painting of the "picture".
*
* @param g The graphics used for painting.
*/
@Override
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.
anthony_p
Offline
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.