Solved

splash screen

Posted on 2004-04-10
14
416 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
basic hardware to learn oop advanced design patterns 3 88
Problem to start Neon 20 89
eclipse java  build path 6 45
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now