I use BufferedImage
, but this is a sample from working code I have.
public class PaintPanel extends JPanel {
private static final long serialVersionUID = 1L;
BufferedImage icon;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(getBufferedIcon(), 12, 12, null);
}
public void setBufferedIcon(BufferedImage icon) {
this.icon = icon;
}
private BufferedImage getBufferedIcon() {
return icon;
}
}
Ignore the offset of 12, that's just because I ripped this from a project of mine. All I do in another class is create the PaintPanel
and set the BufferedIcon
.
Every time I wish to update the icon, I call PaintPanel.setBufferedIcon(icon);
and then PaintPanel.repaint();
Remember to name the PaintPanel
, I'm just calling it that so you can read the code easier :)