java - Url loading error for image in runnable jar -
so, when try following code:
//main class package com.mgflow58.main; import java.awt.image.bufferedimage; import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jframe; public class game { public static void main(string[] args) { bufferedimage mainicon = null; jframe window = new jframe("guppy's adventure"); window.add(new gamepanel()); window.setdefaultcloseoperation(jframe.exit_on_close); window.setresizable(false); window.pack(); window.setlocationrelativeto(null); window.setvisible(true); try { string mainurlstring = "icon.gif"; url mainurl = new url(mainurlstring); try { mainicon = imageio.read(mainurl); window.seticonimage(new imageicon(mainicon).getimage()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
i following error:
java.net.malformedurlexception: no protocol: icon.gif @ java.net.url.<init>(unknown source) @ java.net.url.<init>(unknown source) @ java.net.url.<init>(unknown source) @ com.mgflow58.main.game.main(game.java:28)
the file icon.gif in root folder, , tried copying src/com/mgflow58/main class executing code in. doing wrong? can not set image icon no matter methods try , have been trying find answer while already.
i have simplified answer previous question. have done stupid mistake somewhere, wrote line anew , problem fixed. how accessed icon.gif:
//main class package com.mgflow58.main; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.net.malformedurlexception; import java.net.url; import javax.imageio.imageio; import javax.swing.imageicon; import javax.swing.jframe; public class game { public static void main(string[] args) { bufferedimage mainicon = null; jframe window = new jframe("guppy's adventure"); window.add(new gamepanel()); window.setdefaultcloseoperation(jframe.exit_on_close); window.setresizable(false); window.pack(); window.setlocationrelativeto(null); window.setvisible(true); window.seticonimage(new imageicon(game.class.getresource("/icon.gif")).getimage()); } }
Comments
Post a Comment