All pastes #220121 Raw Edit

Untitled

public text v1 · immutable
#220121 ·published 2006-10-25 01:05 UTC
rendered paste body
public class ILabel extends JLabel implements ImageObserver {
    private Image img;
    public ILabel() {
        super();
    }
    public void setIcon(ImageIcon ico) {
        img = ico.getImage();
        //Should I call super()? Nah.
    }
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        if(img==null)
            return;
        int w = img.getWidth(this);
        int h = img.getHeight(this);
        if(w<=getWidth() && h<=getHeight()) {
            double scalefactor = (double)Math.max(w, h) / Math.max(getWidth(), getHeight());
            w = (int)scalefactor * w;
            h = (int)scalefactor * h;
            img = img.getScaledInstance(w, h, Image.SCALE_SMOOTH);
        }
        g.drawImage(img, w, h, this);
    }
}