|
-
Dec 16th, 2006, 04:06 PM
#1
Thread Starter
Frenzied Member
Splash Screen
I have a splash screen with a label that is used as an image. I want to pause the frame to be on the screen for about 3 seconds then go to the login screen.
This is the code i'm using:
Code:
private void formWindowActivated(java.awt.event.WindowEvent evt) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point startPoint = new Point();
startPoint.setLocation((screenSize.width / 3), (screenSize.height / 3));
this.setLocation(startPoint);
Runnable runner = new Runnable()
{
public void run()
{
try
{
Thread.sleep(5000);
}
catch(Exception ex)
{
System.out.println("Error has occurred: " + ex);
}
}
};
Thread newThread = new Thread(runner);
newThread.start();
LoginFrame newLoginFrame = new LoginFrame();
newLoginFrame.setVisible(true);
this.dispose();
}
It doesn't run that thread with the sleep command. It loads and it doesn't even draw the label picture and goes directly to the login screen. Anyway I can have it load the picture and pause?
-
Dec 16th, 2006, 08:32 PM
#2
Re: Splash Screen
How do you know it doesn't run the thread? The defining thing about threads is that they run in parallel. If you have a thread that just sleeps for five seconds, the only way to know it's running is to let the debugger list the active threads within those five seconds.
It certainly won't delay code coming after the thread.start().
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 16th, 2006, 08:51 PM
#3
Thread Starter
Frenzied Member
Re: Splash Screen
I don't think it runs the thread just because it doesn't pause for 5 seconds. The frame loads, blinks for a second and loads the login frame.
-
Dec 17th, 2006, 06:59 AM
#4
Re: Splash Screen
Then you misunderstand the fundamental principle of a thread. Read Sun's threading tutorial.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 17th, 2006, 01:12 PM
#5
Thread Starter
Frenzied Member
Re: Splash Screen
I just read it and I thought I was doing everything correctly.. this is what I have now though:
Code:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point startPoint = new Point();
startPoint.setLocation((screenSize.width / 3), (screenSize.height / 3));
this.setLocation(startPoint);
jLabel1.setIcon(new javax.swing.ImageIcon("Extras\\splash.GIF"));
Runnable pauseSplashScreen = new Runnable()
{
public void run()
{
try
{
Thread.sleep(4000);
LoginFrame newLoginFrame = new LoginFrame();
newLoginFrame.setVisible(true);
}
catch(Exception ex)
{
System.out.println("Thread was interrupted. Message: " + ex);
}
}
};
Thread pauseScreen = new Thread(pauseSplashScreen);
pauseScreen.start();
while(pauseScreen.isAlive())
{
if(!pauseScreen.isAlive())
{
this.dispose();
}
}
The only problem I run into now is when I should or how I can dispose of the current frame. I can't insert this.dispose() into the Runnable object because it doesn't have reference to that instance of the splash screen. I tried just putting this.dispose(); after the pauseScreen.start() line but that just instantly closed the frame and waited 4 seconds then opened the login frame. I tried that loop and now it just freezes and never closes. How can I do this?
-
Dec 17th, 2006, 03:15 PM
#6
Re: Splash Screen
Explain again why you need to delay the startup of your app (as if Java didn't start slowly enough) just so you can show a splash screen for four seconds while doing nothing? Besides, the architecture you have is still, forgive me the strong expression, idiotic. You create a thread that sleeps for 4 seconds, while the thread that created it does busy waiting for it to finish.
If you want to achieve what you're doing, i.e. display the splash screen for 4 long, annyoing seconds, don't use a second thread. Just create the splash, call Thread.sleep(), dispose the splash and create the login frame.
If, on the other hand, you want to do something sensible, create the splash, do whatever other initialization your application needs, create the login frame, pack() it, then dispose of the splash and display the login frame. Then the splash serves its actual purpose: showing something while your application is loading.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 17th, 2006, 04:18 PM
#7
Thread Starter
Frenzied Member
Re: Splash Screen
First of all, this is just a project for a class. As I know Java is already slow and I wouldn't write this crappy code anywhere else, I am also new to Java so please bear with me as I'm trying to learn exactly what you are talking about. As the program is rather small and doesn't have much of an initialization time, that is why I was going to pause it for a second or two (4 seconds was just a random number I threw in there).
As for your first choice.. I have tried to create the splash, pause it for a certain amount of time, and dipose of it and show the login frame. The problem with that is the frame doesn't initialize the label iconimage and is just a blank frame. Thats the reason why I heard threads will allow it to initialize and also pause it at the same time. I guess I was wrong.
Second, what exactly does the pack() method do? The only thing my program needs to do at first is just show the login frame, so I'm sure there is nothing else i could put in there that would show the splash frame for a long enough time to view the picture.
-
Dec 17th, 2006, 04:56 PM
#8
Re: Splash Screen
For the first, you'd have to set up the thing completely before pausing the thread. I'm pretty sure that would work.
As for the second, pack() does the layout: it decides where every subwindow should go, what the minimum size for the window is, all that stuff. Depending on the layout manager, this can take a moment, which is why it should be done before hiding the splash.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|