Though technically you can paint on a JLabel
, it is preferable to paint on a JPanel
. The code you have seems to be fine, just transer it to a JPanel
instead.
"But there I have the issue, that the Label has basically no size, since it has no actual content."
When painting, you want to remember to override getPreferredSize()
also. paint isn't really content, so there will no preffered size. The reason, it works with by passing a parameter is that the String you pass to it is content, and that makes make the preferred size of the label match the String you pass to it. But like I said, use a JPanel
like this
public class ImagePanel extends JPanel {
private final int D_W = 400;
private final int D_W = 400;
private BufferedIMage imag;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
}