View Single Post
  #4 (permalink)  
Old 07-26-2008, 08:12 PM
hardwired's Avatar
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Calling setSize doesn't do any good till after realization. Override the getPreferredSize method to provide a size hint to the image components parent.
Code:
class ImagePanel extends Panel {
    private Image im;

    public ImagePanel(Image img){
        im = img;
//        setSize(im.getWidth(this),im.getHeight(this));
    }

    public void paint(Graphics g){
        g.drawImage(im,0,0,this);
    }

    public Dimension getPreferredSize() {
        return new Dimension(im.getWidth(this),im.getHeight(this));
    }
}
Reply With Quote