Log in

View Full Version : how to create splash screen


wrappingduke
03-21-2008, 09:30 PM
Hello,

Attempting to create splash screen on Creme Emulator. I'm using JSR-62. I've been able to successfully use this splash screen on J2SE using JDK 1.3.
However, in the emulator the frame of the splash screen is shown but the window w/ the image is not shown until the splash screen is being disposed. That is, the frame is shown and then image flashes and goes away as the frame is being disposed.

The Splash screen is a frame that contains a window and the window has the image. Here's the code I'm using:


import java.awt.event.*;
import java.awt.*;
import java.net.URL;

publicfinalclass SplashScreen extends Frame {

privatefinal String fImageId;
private MediaTracker fMediaTracker;
private Image fImage;

public SplashScreen(String aImageId) {
/* Implementation Note
* Args.checkForContent is not called here, in an attempt to minimize
* class loading.
*/
if ( aImageId == null || aImageId.trim().length() == 0 ){
thrownew IllegalArgumentException("Image Id does not have content.");
}
fImageId = aImageId;
}

publicvoid splash(){
initImageAndTracker();
setSize(fImage.getWidth(null), fImage.getHeight(null));
center();

fMediaTracker.addImage(fImage, 0);
try{
fMediaTracker.waitForID(0);
}
catch(InterruptedException ie){
System.out.println("Cannot track image load.");
}

SplashWindow splashWindow = new SplashWindow(this,fImage);
}

privatevoid initImageAndTracker(){
fMediaTracker = new MediaTracker(this);
URL imageURL = SplashScreen.class.getResource(fImageId);
fImage = Toolkit.getDefaultToolkit().getImage(imageURL);
}

privatevoid center(){
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle frame = getBounds();
setLocation((screen.width - frame.width)/2, (screen.height - frame.height)/2);
}

privateclass SplashWindow extends Window {
private Image fImage;

SplashWindow(Frame aParent, Image aImage) {
super(aParent);
fImage = aImage;
setSize(fImage.getWidth(null), fImage.getHeight(null));
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle window = getBounds();
setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
setVisible(true);
}
publicvoid paint(Graphics graphics) {
if (fImage != null) {
graphics.drawImage(fImage,0,0,this);
}
}
}

/**
* Developer test harness shows the splash screen for a fixed length of
* time, without launching the full application.
*/
privatestaticvoid main(String[] args){
SplashScreen splashScreen = new SplashScreen("images/StocksMonitor.gif");
splashScreen.splash();
try{
Thread.sleep(2000);
}
catch(InterruptedException ex) {
System.out.println(ex);
}
splashScreen.dispose();
}
}

//Here is an example of a class which launches an application using the above SplashScreen
publicstaticvoid main(String args[]) {
fSplashScreen = new SplashScreen("images/MyImage.gif");
fSplashScreen.splash();

try
{
Thread.sleep(2000);
}
catch(.....){}

java.awt.EventQueue.invokeLater(new Runnable() {
publicvoid run() {
fSplashScreen.dispose()
}
});



any help is appreciated

albert_kam
05-15-2008, 10:13 AM
Aha ! A Java Buddy that uses Creme too (You should know Rene, right ? haha), and using the same splash screen tutorial also ! What a coincidence, lol

Anyway, i tried the splash screen tutorial also, to find out that the splashing just shows for a while like your experience.

I tried several things, and found out that to achieve the purpose of showing it till the main frame is 'realized' (shown and drawed on screen), i have to put the line SplashWindow.dispose() in the main method in my main app class.

In my case, it is like this :

public static void main(String[] args) {
// load JNI stuffs code

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MySwingApp().setVisible(true);
SplashWindow.dispose();
}
});

// set the app to foreground code
}

Good luck buddy !

Cheers,
Albert Kam

wrappingduke
06-24-2008, 06:35 PM
Hi Albert,

Thx. for the response. I apologize for the belated response(well over a month, now). I've going between different projects.

I've tried what you have suggested, i.e. placing the call to dispose() after run() call and I ended up w/ the same results. That is, the frame is shown w/out the image.

However, I did pause the splashwindow thread by invoking wait() and of course, the image was then visible in the frame. It appears the image does not have a chance to paint in the frame, so I tried overriding the update method, so repaint() could be called and subsequently paint. Unfortunately, did not work either.

Any suggestions are appreciated.

Kind regards,

Jim