Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

splash screen

Posted on 2004-04-10
14
Medium Priority
?
442 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 200 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

Industry Leaders: 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …
Suggested Courses

604 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