java - Swing - Repainting of a Photo on JPanel -
hi guys, kindly ask me .
properly need refresh jpanel
different photo gotten files. 1st time during adding of jpanel
photo on frame - photo showed correctly ! ok
but when try change current photo dynamically 1 , refresh jpanel
- see same (old) photo. , not matter place following "refreshing" part of code used:
picturepanel.repaint();
picturepanel.validate();
you can find below code:
// create own jpanel public class imagepanel extends jpanel { private image image; public image getimage() { return image; } public void setimage(image image) { this.image = image; } @override public void paintcomponent(graphics g) { super.paintcomponent(g); if (image != null) { g.drawimage(image, 0, 0, getwidth(), getheight(), null); } else system.out.println("the picture missing!"); } }
get photo file , add own jpanel (imagepanel)
public jpanel gettestpicture(string fromfile) { imagepanel pp = new imagepanel(); pp.setlayout(new borderlayout()); try { pp.setimage(imageio.read(new file(fromfile))); } catch (ioexception e) { e.printstacktrace(); } return pp; }
and main call of jpanel:
picturepanel=gettestpicture("picture.jpg"); frame.add(picturepanel); //looks correct - photo visible.
....
if trying repaint jpanel once more during program old photo stayed on panel. new photo not painted.
picturepanel=gettestpicture("picture.jpg"); frame.add(picturepanel); //picture.jpg - it`s showed correctly! picturepanel=gettestpicture("pic2.jpg"); picturepanel.repaint(); picturepanel.validate(); //doesn`t work ! picture.jpg on jpanel still !
please people me ! need understand whats wrong in code! please don`t propose use jlabel or similar.
thank in advance !!!!!
don't add new imagepanel
frame, update existing one...
public class someothercomponent extends jpanel { private imagepanel imagepanel; //... public someothercomponent() { //... imagepane = gettestpicture("picture.jpg"); add(imagepane); //... }
when need change image, use like
imagepane.setimage(imageio.read(...)); imagepane.repaint();
Comments
Post a Comment