Solved

splash screen

Posted on 2004-04-10
14
422 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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