java - call paintComponet agian -


i make imagepanel class imageicon object , draw show panel.

import javax.swing.jpanel; import javax.swing.imageicon; import java.awt.graphics;  class imagepanel extends jpanel {     private imageicon img;      public imagepanel(imageicon img) {         this.setimage(img);     }      public void setimage(imageicon img){         this.img = img;      }      @override     public void paintcomponent(graphics g) {         if(img instanceof imageicon)             g.drawimage(img.getimage(), 0, 0, this.getwidth(), this.getheight(), null);     } } 

but problem when change img, don't show on panel till change frame size. how can update it?

edit: repaint() not clean last img on panel.

typically need call repaint within setimage method.

you should calling super.paintcomponent in order prevent possibility of introducing paint artifacts rendering process.

you should should consider overriding getpreferredsize in order ensure component laid out under layout managers. should reflect size of img.

unless have reason otherwise, away using jlabel


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -