java - Adding a image to the JPanel -
i'm trying add hangman image jpanel. have labeled image 1-10 , increment value each time user gets wrong answer (this working). of yet haven't been able image work. how insert image?
if(!found){   numerror++;    string usererror = integer.tostring(numerror);   string jpg = usererror.concat(".jpg");   try{     bufferedimage myimg = imageio.read(new file(jpg));     jlabel hangman = new jlabel(new imageicon(myimg));     hangman.setsize(200,100);     hangman.setlocation(300, 20);     add(hangman);   }catch(ioexception ex){     ex.printstacktrace();   }  } javax.imageio.iioexception: can't read input file! @ javax.imageio.imageio.read(imageio.java:1301) @ hangmanpanel$1.actionperformed(hangmanpanel.java:73) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6505) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321) @ java.awt.component.processevent(component.java:6270) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4861) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4687) ... 
since image become application resource embedded in jar file, image must accessed url.  see embedded resource info. page overview of how form url.  tricky thing left determine string use.
i have .jpg files place in main dir of both src , bin folder.
ok, if image called hungman.jpg string find "/hungman.jpg" 
especially note
- the leading /tells class-loader search 'the root of class-path'.
- the case of name must right.  not matter windows , fileinstances, urls.
note: accessing image url work either in jar or long image on run-time class-path of application. if ide not automatically jar classes , resources, should still work.
Comments
Post a Comment