Set Icon Image in Java -
i have been searching everywhere on how set icon image in java, , ends not working or gives me errors. here, in main method put code:
public static void main(string[] args) { game game = new game(); // right here! game.frame.seticonimage(new imageicon("/icon.png").getimage()); game.frame.setresizable(false); game.frame.settitle(title); game.frame.add(game); game.frame.pack(); game.frame.setdefaultcloseoperation(jframe.exit_on_close); game.frame.setlocationrelativeto(null); game.frame.setvisible(true); }
my path image "%project%/res/image.png" , use /image.png go ahead , access res folder (as have done in other parts of project) have converted icon file, , tried that, decides use default java icon.
your problem due looking in wrong place image, or if classes , images in jar file, looking files files don't exist. suggest use resources rid of second problem.
e.g.,
// path must relative *class* files string imagepath = "res/image.png"; inputstream imgstream = game.class.getresourceasstream(imagepath ); bufferedimage myimg = imageio.read(imgstream); // imageicon icon = new imageicon(myimg); // use icon here game.frame.seticonimage(myimg);
Comments
Post a Comment