image - Reloaded file not change in java -


im try load image file chooser, when updated image after loaded, , reload it, image not change. show first time loaded. (i tryed not use filechooser)

load-> change image -> load -> image not change

load-> change image ->exit program ->open program -> load -> image change

how can clear it?

load image

         int ret = chooser.showopendialog(null);              if(ret == jfilechooser.approve_option){              string filepath = chooser.getselectedfile().getpath();              imageicon icon = new imageicon(filepath);              main.changeimage(icon);          } 

change image

public void paintcomponent(graphics g) {     graphics2d g2 = (graphics2d) g;     g2.drawimage(image.getimage(), 0, 0, this);  }  public void changeimage(imageicon img) {     image = img;     origin_image = image;     origin_height = image.geticonheight();     origin_width = image.geticonwidth();      this.setbounds(0, 0, image.getimage().getwidth(null), image.getimage()             .getheight(null));     repaint(); } 

java tends cache images.

if images change during run-time, 1 way fool methods reloading read bytes of file yourself, form input stream them (using bytearrayinputstream) , use input stream in imageio.read(inputstream).

the reason works if url or file used load image, toolkit uses location key cache it.


other notes

public void paintcomponent(graphics g) {     graphics2d g2 = (graphics2d) g;     // .. 

should be:

public void paintcomponent(graphics g) {     super.paintcomponent(g); // paint border , bg avoid arttifacts!     graphics2d g2 = (graphics2d) g;     // .. 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -