Pregunta

I've found an example in Oracle docs about SplashScreen. the problem is in this example the link of the image used here is passed as argument in the command line. I'm trying to change the code so the link is written inside and I don't need to use the command line.

methode setImageURL(URL imageURL) should be able to do the work for me, but it's not accepting my argument (parameter).

I read about URL class, seems like it needs protocol! protocol like http and ftp ? if that's the case, how should my url be for files in my computer ? when I try to put a link from my computer (ex: "C:\plash.gif") it says illege excape character

I even tried to use http link for an image but it give me this error within the URL line:

non-static method setImageURL(URL) cannot be referenced from a static context

here's the code:

package misc;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class SplashDemo extends Frame implements ActionListener {
    static void renderSplashFrame(Graphics2D g, int frame) {
        final String[] comps = {"foo", "bar", "baz"};
        g.setComposite(AlphaComposite.Clear);
        g.fillRect(120,140,200,40);
        g.setPaintMode();
        g.setColor(Color.BLACK);
        g.drawString("Loading "+comps[(frame/5)%3]+"...", 120, 150);
    }
    public SplashDemo() {
        super("SplashScreen demo");
        setSize(300, 200);
        setLayout(new BorderLayout());
        Menu m1 = new Menu("File");
        MenuItem mi1 = new MenuItem("Exit");
        m1.add(mi1);
        mi1.addActionListener(this);
        this.addWindowListener(closeWindow);

        MenuBar mb = new MenuBar();
        setMenuBar(mb);
        mb.add(m1);
        URL link= new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
        SplashScreen.setImageURL(link);
        final SplashScreen splash = SplashScreen.getSplashScreen();
        if (splash == null) {
            System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
        }

        Graphics2D g = splash.createGraphics();
        if (g == null) {
            System.out.println("g is null");
            return;
        }
        for(int i=0; i<100; i++) {
            renderSplashFrame(g, i);
            splash.update();
            try {
                Thread.sleep(90);
            }
            catch(InterruptedException e) {
            }
        }
        splash.close();
        setVisible(true);
        toFront();
    }
    public void actionPerformed(ActionEvent ae) {
        System.exit(0);
    }

    private static WindowListener closeWindow = new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            e.getWindow().dispose();
        }
    };

    public static void main (String args[]) {
        SplashDemo test = new SplashDemo();
    }
}

this is the output:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.net.MalformedURLException; must be caught or declared to be thrown
    at misc.SplashDemo.main(SplashDemo.java:103)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

and nothing happens.

P.S: I'm a very beginner with Java, I'm using NetBeans IDE 7.2.1

¿Fue útil?

Solución

The splash screen can be displayed at application startup, before the Java Virtual Machine (JVM) starts.

The splash screen window is closed automatically as soon as the first window is displayed by Swing/AWT (may be also closed manually using the Java API, see below).

If your application is packaged in a jar file, you can use the

SplashScreen-Image option in a manifest file to show a splash screen.
Place the image in the jar archive and specify the path in the option.
The path should not have a leading slash. For example, in the manifest.mf file:

 Manifest-Version: 1.0
 Main-Class: Test
 SplashScreen-Image: filename.gif

The SplashScreen class provides the API for controlling the splash screen. This class may be used to

  • close the splash screen
  • change the splash screen image
  • get the splash screen native window position/size
  • paint in the splash screen.

It cannot be used to create the splash screen.

  • This class cannot be instantiated.
  • Only a single instance of this class can exist, and it may be obtained by using the getSplashScreen() static method.

In case the splash screen has not been created at application startup via the command line or manifest file option,

the getSplashScreen method returns null.

so what is wrong with your code?

NOT

SplashScreen.setImageURL(link);

OK

splash.setImageURL(link);

wrong sequence : Setting ImageUrl before you have a splash Object

URL link= new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
        SplashScreen.setImageURL(link);
        final SplashScreen splash = SplashScreen.getSplashScreen();

correct : Get splash and then set ImageUrl

short

final SplashScreen splash = SplashScreen.getSplashScreen();
splash.setImageURL(link);

long with catch MalformedURLException to get rid of the error
MalformedURLException : must be caught

final SplashScreen splash = SplashScreen.getSplashScreen();
if (splash == null) {
       System.out.println("SplashScreen.getSplashScreen() returned null");
            return;
   }
URL link;
   try {
        link = new URL("http://docs.oracle.com/javase/tutorial/uiswing/examples/misc/SplashDemoProject/src/misc/images/splash.gif");
       } catch (MalformedURLException ex) {
            System.out.println("MalformedURLException link:77");
            return;
       }
   try {
            splash.setImageURL(link);
       } catch (NullPointerException | IOException | IllegalStateException ex) {
            System.out.println("NullPointer or IO or IllegalState setImageUrl:85");
            return;
       }

To recognize the difference between the local image file and the image file on the internet . I have made a local blue splash.gif file.

The proceeding is as follows.

  • Local image is loaded. (SplashScreen-Image option in the manifest file)

enter image description here

enter image description here

  • Application appears.

enter image description here


To get it to work in Netbeans

and not always get the error SplashScreen.getSplashScreen() returned null

final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
           System.out.println("SplashScreen.getSplashScreen() returned null");

you must do the following.

in properties point to your local .gif file : -splash:src/misc/images/splash.gif

enter image description here

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top