How to add an image to an object in Java? -
this question has answer here:
- load image filepath via bufferedimage 5 answers
i'm trying add proper images each card object, can't seem work. tried incorporate filldeck() method, not sure best way it, be.
here's got:
public void filldeck() { iterator suititerator = cardsuit.values.iterator(); while(suititerator.hasnext()) { cardsuit suit = (cardsuit) suititerator.next(); iterator rankiterator = cardvalue.values.iterator(); while(rankiterator.hasnext()) { cardvalue value = (cardvalue) rankiterator.next(); /* problem area :l */ string imagefile = imagelocation + card.getimagefilename(suit, value); imageicon image = new imageicon(getimage(getcodebase(), imagefile)); /* --------------------------- */ card card = new card(suit, value, image); addcard(card); }}}
image location initialized as: private final string imagelocation = "cardimages/";
and getimagefilename follows:
public static string getimagefilename( cardsuit suit, cardvalue value ) { return suit.getsuitacronym() + value.getvalueacronym() + ".png"; }
i checking out imageio, not quite sure on how implemented this. here's file tree, have images folder in wrong place: http://i.stack.imgur.com/jt6po.png
so yeah, sorry long post, can provide insight on how should go or if can spot mistakes.
to load image in java:
bufferedimage image = null; try{ image = imageio.read(new file("location.png")); } catch(exception e){e.printstacktrace();}