Hi people, I am having trouble with my splash screen. Basically, it just flashes really quickly and I can't get to to pause for long enough. See attached file.

I've attached my program as a zipped file.

After you have download it you need to right click and extract it otherwise it won't work.

Awaiting your earliest reply.

God bless.

Here's the code:

import java.awt.*;
import java.awt.event.*;

public class TestSplash {
  MyFrame theFrame;


  public static void main (String args[]){
    TestSplash t = new TestSplash();
    t.createMainFrame();
    }

  private void createMainFrame() {
    theFrame = new MyFrame("A Dummy Frame");
    theFrame.setVisible(true);
    }

}

class MyFrame extends Frame {
  Splash mySplash;
  public MyFrame(String title){
    super(title);
    addWindowListener
      (new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
          }
        }
       );
    mySplash = new Splash(this, "blart.png");

    // dummy delay so we can see the Splash!
    for(int i = 0; i < 50000; i++) { //PROBLEM HERE
      System.out.println(i) ;
      }
    setSize(200,200);
    }
  }

Don't tell me you are creating a pause with System.out.println() ? Use threads and make it sleep.

Can you show me an example using a thread? I don't see how that would make a difference? Explain?

It'll make a huge difference.

class Test implements Runnable
{
   public Test()
   {
     super();

     Thread t = new Thread(this); 
     t.start();
     //t.run();  can't remember if you have to call this or not
   }

   public void run()
   {
       while (true)
       {
           try
           {
              System.out.println("In Run");
              Thread.sleep(2000);
           }
           catch(InterruptedException ie)
           {
           }
     }
  }
}

It's not tested, but if you simply want a delay then use threads.

It'll make a huge difference.

class Test implements Runnable
{
   public Test()
   {
     super();

     Thread t = new Thread(this); 
     t.start();
     //t.run();  can't remember if you have to call this or not
   }

   public void run()
   {
       while (true)
       {
           try
           {
              System.out.println("In Run");
              Thread.sleep(2000);
           }
           catch(InterruptedException ie)
           {
           }
     }
  }
}

It's not tested, but if you simply want a delay then use threads.

Thanks for your help but I still don't see how mine won't work.

Here is the other part of the code. Please try it or see attachment for an example. Do you see what I mean?

//Splash.java
import java.awt.*;
import java.awt.event.*;

public class Splash extends Window {
  private Image splashImage;
  private int imgWidth, imgHeight;
  private String imgName;
  private static final int BORDERSIZE = 0;
  private static final Color BORDERCOLOR = Color.WHITE;
  Toolkit tk;

  public Splash(Frame f, String imgName) {
    super(f);
    this.imgName = imgName;
    tk = Toolkit.getDefaultToolkit();
    splashImage = loadSplashImage();
    showSplashScreen();
    f.addWindowListener(new WindowListener());
    }
  public Image loadSplashImage() {
    MediaTracker tracker = new MediaTracker(this);
    Image result;
    result = tk.getImage(imgName);
    tracker.addImage(result, 0);
    try {
      tracker.waitForAll();
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    imgWidth = result.getWidth(this);
    imgHeight = result.getHeight(this);
    return (result);
    }

  public void showSplashScreen() {
    Dimension screenSize = tk.getScreenSize();
    setBackground(BORDERCOLOR);
    int w = imgWidth + (BORDERSIZE * 2);
    int h = imgHeight + (BORDERSIZE * 2);
    int x = (screenSize.width - w) /2;
    int y = (screenSize.height - h) /2;
    setBounds(x, y, w, h);
    setVisible(true);
    }

  public void paint(Graphics g) {
    g.drawImage(splashImage, BORDERSIZE, BORDERSIZE,
      imgWidth, imgHeight, this);
    }

  class WindowListener extends WindowAdapter {
    //  was windowActivated, thanks to H.Grippa for the fix!
    public void windowOpened(WindowEvent we) {
      setVisible(false);
      dispose();
      }
    }
  }
//TestSplash.java
import java.awt.*;
import java.awt.event.*;

public class TestSplash {
  MyFrame theFrame;


  public static void main (String args[]){
    TestSplash t = new TestSplash();
    t.createMainFrame();
    }

  private void createMainFrame() {
    theFrame = new MyFrame("A Dummy Frame");
    theFrame.setVisible(true);
    }

}

class MyFrame extends Frame {
  Splash mySplash;
  public MyFrame(String title){
    super(title);
    addWindowListener
      (new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          System.exit(0);
          }
        }
       );
    //file needs to be jpeg or png
    mySplash = new Splash(this, "blart.png");
    Thread.sleep(2000);
    // dummy delay so we can see the Splash!
    //for(int i = 0; i < 50000; i++) {
      //System.out.println(i) ;
      //}
    setSize(200,200);
    }
  }

That won't work the way you have. You MUST implement Runnable and override the run() method. Inside the run method is where you create the delay.

That won't work the way you have. You MUST implement Runnable and override the run() method. Inside the run method is where you create the delay.

You haven't tried it have you? There is an executable in my attachment which should emphasise my point. LOL.

I mean to say, it works on my computer but fails to work on my friends computer. Has anyone else tried it? And I know she has Java run time environment on hers but some appears to be wrong.

Please try my code. The main class is TestSplash.java

Thanks again,your help is appreciated...

I got a frame called a dummy frame, but that was it. Nothing was on the frame or anything. Is your friend recieving an error message? Does the console icon appear in the system tray?

I got a frame called a dummy frame, but that was it. Nothing was on the frame or anything. Is your friend recieving an error message? Does the console icon appear in the system tray?

You need to make sure the path to your splash screen image is actually there.

For example

mySplash = new Splash(this, "blart.png");

would be changed to:

mySplash = new Splash(this, "c:\yourpic.png");

As long as the picture is actually in your c: directory and is of a png or jpeg form it should display. Tell me if this works for you. Thanks

[Edit] To illustrate my point, I have kindly attached a zip file. Which should contain the executable and the png picture. However, in order for the path to be recognised you have to EXTRACT the zipped file. Which can be done by right clicking the folder and selecting extract all... Assuming of course, you are using windows as your OS [/Edit]

Can you see what the problem is server crash? Did it work what I said. I followed instructions you gave me previously about creating a jar executable file, and the manifest crap. Is that working?

And the splash screen image I coded using javaOpengl, however, the rendering was a bit sluggish. It that took ages but I just followed the tutorials. Simple really.

Thank you for all your help :D

It worked fine for me. The manifest was good, the splash was good, etc... If it's still not working on your friends computer then maybe check what JRE you are running and compare it to what they are running. Maybe try right clicking on the jar file on the other computer and see if it's running it with javaw.

mySplash = new Splash(this, "blart.png");


// dummy delay so we can see the Splash!
for(int i = 0; i < 50000; i++) { //PROBLEM HERE
System.out.println(i) ;
}



Use the next code instead of for loop:


while(true)
{
if(new File("test.test").exists())
{
break;
}
else
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e){}
}
}

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.