java - Changing SplashScreen Image with SplashScreen.setImageURL(link); -
i've found example in oracle docs splashscreen. problem in example link of image used here passed argument in command line. i'm trying change code link written inside , don't need use command line.
methode setimageurl(url imageurl)
should able work me, it's not accepting argument (parameter).
i read url class, seems needs protocol! protocol http , ftp ? if that's case, how should url files in computer ? when try put link computer (ex: "c:\plash.gif"
) says illege excape character
i tried use http link image give me error within url line:
non-static method setimageurl(url) cannot referenced static context
here's 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 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 output:
exception in thread "main" java.lang.runtimeexception: uncompilable source code - unreported exception java.net.malformedurlexception; must caught or declared thrown @ misc.splashdemo.main(splashdemo.java:103) java result: 1 build successful (total time: 4 seconds)
and nothing happens.
p.s: i'm beginner java, i'm using netbeans ide 7.2.1
the splash screen can displayed @ application startup, before java virtual machine (jvm) starts.
the splash screen window closed automatically first window displayed swing/awt (may closed manually using java api, see below).
if application packaged in jar file, can use
splashscreen-image option in manifest file show splash screen.
place image in jar archive , specify path in option.
path should not have leading slash. example, in manifest.mf file:
manifest-version: 1.0 main-class: test splashscreen-image: filename.gif
the splashscreen class provides api controlling splash screen. class may used
- close splash screen
- change splash screen image
- get splash screen native window position/size
- paint in splash screen.
it cannot used create splash screen.
- this class cannot instantiated.
- only single instance of class can exist, , may obtained using getsplashscreen() static method.
in case splash screen has not been created @ application startup via command line or manifest file option,
the getsplashscreen method returns null.
so wrong code?
not
splashscreen.setimageurl(link);
ok
splash.setimageurl(link);
wrong sequence : setting imageurl before have 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 : splash , set imageurl
short
final splashscreen splash = splashscreen.getsplashscreen(); splash.setimageurl(link);
long catch malformedurlexception
rid of error
malformedurlexception : must 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 difference between local image file , image file on internet . have made local blue splash.gif file.
the proceeding follows.
- local image loaded. (splashscreen-image option in manifest file)
- internet image loaded. (//docs.oracle.com...images/splash.gif)
- application appears.
to work in netbeans
and not error splashscreen.getsplashscreen() returned null
final splashscreen splash = splashscreen.getsplashscreen(); if (splash == null) { system.out.println("splashscreen.getsplashscreen() returned null");
you must following.
in properties point local .gif file : -splash:src/misc/images/splash.gif
Comments
Post a Comment