Solved

splash screen

Posted on 2004-04-10
14
415 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 62
topping2 challenge 13 61
JAVA part two 5 41
ArrayIndexOutOfBoundException 9 35
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

759 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

17 Experts available now in Live!

Get 1:1 Help Now