java - adding JFrame background caused .JAR to be broken -
i want know problem in code caused .jar file broken? when double click on jar file doesn't open fine before adding background image jframe.
i created class called cube draw cube , want set background image jframe, added field in cube class
private bufferedimage bg;
bg initialization in class constructor
publice cube() { bg = imageio.read(new file("bg_image.jpg")); }
this piece of code used set background
@override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(bg, 0, 0, this); }
in main class created jframe object draw cube
jframe jf = new jframe(); jf.add(new cube()); jf.setenabled(true); jf.setdefaultcloseoperation( jframe.exit_on_close ); jf.setresizable(false); jf.setsize( 500, 500 ); jf.setvisible( true );
since not have jar file there no way me know problem. need access file resource , not new file because inside jar. first should use cmd run jar , see if gives stack trace. use command: java -jar filename.jar
run jar via command prompt.
try using:
cube.class.getresourceasstream("bg_image.jpg");
Comments
Post a Comment