Painting a Swing component over a SplashScreen

During startup we want to render some extra text onto the splash screen. I know I can do this directly, but figuring out all the offsets for the text is a PITA (it's styled, at various sizes, and may be different heights depending on the particular user running the app, ...)

So I thought I might be able to able to cut corners by using our existing SplashPanel which lays out some JLabels, and rendering that onto the graphics for the splash screen.

publicstaticvoid addSplashDetails(){

SplashScreen splash = SplashScreen.getSplashScreen();

Dimension size = splash.getSize();

Graphics2D graphics = splash.createGraphics();

try{

SplashPanel panel =new SplashPanel("Starting");

panel.setSize(size);

panel.validate();

panel.paint(graphics);

}finally{

graphics.dispose();

}

}

When I run this, the initial splash image comes up and then in a couple more seconds, the screen turns to a grey box. It turns out to be the paint(Graphics) call which is creating the grey box. The SplashPanel class actually paints the same background image as the one I'm using for the splash, so I'm confused as to why it would be greyed out.

Ideas, anyone?

[1625 byte] By [trejkaza] at [2007-11-26 18:38:56]
# 1

trejkaz!

I see you have posted loads more than I so I may be telling you something you already know. I was under the impression that Graphics2D objects don't last long and so anything you paint in them gets eventually lost when a repaint or paint method is called.

That being the case, rather than placing your code inside a method to paint, would it be better to override paint/repaint and after a super.repaint() add the details into there. So whenever paint/repaint is called, you will have the extra details added as part of the call and not wiped over like it appears to be now (the grey box is an empty one...)

Let me know what you think. (and more importantly if I was under the correct impression or just totally wrong! lol)

Jason.

fireman.sparkeya at 2007-7-9 6:12:58 > top of Java-index,Desktop,Core GUI APIs...
# 2
Nah that's not how the SplashScreen API works. The graphics context it gives you actually paints onto a transparent overlay image so it will stick around. Check out the Javadocs.
trejkaza at 2007-7-9 6:12:58 > top of Java-index,Desktop,Core GUI APIs...
# 3
You can use the paint method only if SplashPanel is visible, which from your code looks like it's not.
Rodney_McKaya at 2007-7-9 6:12:58 > top of Java-index,Desktop,Core GUI APIs...
# 4

It's visible.

The splash screen is visible until some other window is displayed. Since in my code no other window is displayed, I don't see how you would think that my code looks like it would hide it. (It's almost as if nobody actually knows how the splash screen works, judging from the responses so far.)

I can paint other things (e.g. lines) onto it without any issues.

trejkaza at 2007-7-9 6:12:58 > top of Java-index,Desktop,Core GUI APIs...
# 5

1. I'm not using java 6 so I don't work with splash screen.

2. I was not taking about the splash screen being visible, but the panel that you try to use to paint on the splash screen - your SplashPanel.

3. No way I can tell that the SplashPanel is visible from the code you provided.

4. If you want further help post a SSCCE.

Rodney_McKaya at 2007-7-9 6:12:58 > top of Java-index,Desktop,Core GUI APIs...
# 6

Yeah, the most I can do with SplashPanel is to make it do layout which is supposed to validate the component, but that's as far as I can go without adding it to a window and making it actually visible. Problem is as soon as something becomes visible the real splash vanishes, so it becomes impossible to draw on it.

I can't go the route of overriding a paint method like with normal AWT or Swing, because SplashScreen isn't a real component. If it were a real component, I wouldn't have this issue because I could add my labels directly to it. :-)

It seems like the lowest barrier is to manually calculate the text sizes and manually position them. This produces a workable solution but it's a bit of a pain that we even have to since text layout is a royal pain. I guess if nothing else it might mean that the Swing classes don't need to load in order to paint my text. :-(

trejkaza at 2007-7-9 6:12:58 > top of Java-index,Desktop,Core GUI APIs...