Solved

splash screen

Posted on 2004-04-10
14
429 Views
Last Modified: 2010-03-31
Hi!

I have tried to make this splash screen code work taken from
http://www.javaworld.com/javatips/jw-javatip104_p.html

so this is my main application ...I have set it up like this.

 public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }                                                                      //but i'm not sure about this part or if this is the way to call it..
    Splash spl = new Splash("splash.gif",?????,6);  //< what goes here...???you can see the constructor in the last part of link given.
    new Thread(new Runnable() { public void run()
    {
       try { Thread.sleep(10000); } catch (Exception ex) {}
       spl.dispose();
    }}).start();
    new Application1();<<<this is my main app
    System.gc();
  }
}

and I tried another splash which kill my application as soon as its time ran out...why?
Cheers!
0
Comment
Question by:scream_18m
  • 7
  • 7
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10799594
you need to pass it the frame used by your application.
if u don't have a frame then use null.
0
 

Author Comment

by:scream_18m
ID: 10799622
ok so this is the entire ..main class...


public class Application1 {
  boolean packFrame = false;
  //Construct the application
  public Application1() {
    // put this in your main() function
    Logger.getRootLogger().addAppender(
        new NullAppender()
        );  
    Frame1 frame = new Frame1();    <<<I've tried now to set this as global static and pass it in but the app just comes up nothing else
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
      frame.pack();
    }
    else {
      frame.validate();
    }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation( (screenSize.width - frameSize.width) / 2,
                      (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
  }

  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    final Splash spl;                                                     <<complains that it must be final..
        spl = new Splash("splash.gif",null,6);                       <<and I have tried this...
    new Thread(new Runnable() { public void run()
    {
       try { Thread.sleep(10000); } catch (Exception ex) {}
       spl.dispose();
    }}).start();
    new Application1();
    System.gc();
  }
}


so what am I doing wrong...?
0
 
LVL 92

Expert Comment

by:objects
ID: 10799663
Add a getFrame() method to your application class to return the frame.
Create the application *before* you create splash screen and use the getFrame() method to get the frame.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:scream_18m
ID: 10799681
ok made the changes...  not sure if this is what you meant though...but it still does not work..???

public class Application1 {
  boolean packFrame = false;
  static Frame1 frame;
  //Construct the application
  public Application1() {
    // put this in your main() function
    Logger.getRootLogger().addAppender(
        new NullAppender()
        );
    frame = new Frame1();
    //Validate frames that have preset sizes
    //Pack frames that have useful preferred size info, e.g. from their layout
    if (packFrame) {
      frame.pack();
    }
    else {
      frame.validate();
    }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation( (screenSize.width - frameSize.width) / 2,
                      (screenSize.height - frameSize.height) / 2);
    frame.setVisible(true);
  }
 
  public static Frame1 getFrame(){
     return frame;
  }

  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    new Application1();

    final Splash spl;
        spl = new Splash("splash.gif",getFrame(),6);
    new Thread(new Runnable() { public void run()
    {
       try { Thread.sleep(10000); } catch (Exception ex) {}
       spl.dispose();
    }}).start();
   
    System.gc();
  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 10799705
how exactly does it not work?

btw frame does not need to be static.
0
 

Author Comment

by:scream_18m
ID: 10799759

my application starts...but no splash appears

The image is in my working directory so the way I call should be ok...

should I move the splash related stuff inside the applications constructor..just after frame is being created?

0
 
LVL 92

Expert Comment

by:objects
ID: 10799775
post your splash screen code.
0
 

Author Comment

by:scream_18m
ID: 10799783
taken from the link..given before..


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Splash
    extends JWindow {

  public Splash(String filename, Frame f, int waitTime) {
    super(f);
    JLabel l = new JLabel(new ImageIcon(filename));
    getContentPane().add(l, BorderLayout.CENTER);
    pack();
    Dimension screenSize =
        Toolkit.getDefaultToolkit().getScreenSize();
    Dimension labelSize = l.getPreferredSize();
    setLocation(screenSize.width / 2 - (labelSize.width / 2),
                screenSize.height / 2 - (labelSize.height / 2));
    addMouseListener(new MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        setVisible(false);
        dispose();
      }
    });
    final int pause = waitTime;
    final Runnable closerRunner = new Runnable() {
      public void run() {
        setVisible(false);
        dispose();
      }
    };
    Runnable waitRunner = new Runnable() {
      public void run() {
        try {
          Thread.sleep(pause);
          SwingUtilities.invokeAndWait(closerRunner);
        }
        catch (Exception e) {
          e.printStackTrace();
          // can catch InvocationTargetException
          // can catch InterruptedException
        }
      }
    };
    setVisible(true);
    Thread splashThread = new Thread(waitRunner, "SplashThread");
    splashThread.start();
  }
}


0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 10799798
that code already handles closing the splash screen so there is no need for the thread you create in your main.

Looks like the problem is the delay you specify, it is specified in milliseconds so try 6000 instead of 6.
0
 

Author Comment

by:scream_18m
ID: 10799843
still no luck...the applications comes up and no sign of the splash..I've even tried 60000 millisec...

//Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    new Application1();
    Splash spl = new Splash("splash.gif",getFrame(),6000);
    System.gc();
  }
0
 
LVL 92

Expert Comment

by:objects
ID: 10799854
> The image is in my working directory so the way I call should be ok...

Where is your working directory?
0
 

Author Comment

by:scream_18m
ID: 10799874
I restarted the computer...it seems to work now but appears at the same time as my application.. I want it to be as an indication that the program is loading and go away when the app starts..how do I control this.?
0
 

Author Comment

by:scream_18m
ID: 10799917
C:\proviz\src\proviz\   >>here is there source files..    I have a copy of the image here
C:\proviz\          >>and here is the working directory ...and I have a copy of the image here as well.

0
 
LVL 92

Expert Comment

by:objects
ID: 10802675
> an indication that the program is loading and go away when the app starts..how do I control this.?

don't make your app frame visible until your splash screen is hidden.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question