Domanda

I have a class, that extends JLabel, to display an image on the whole innerArea of the label.
To achieve this, I overwrite paintComponent like this:

protected void paintComponent(Graphics g) {

    super.paintComponent(g);

    if(image != null) {
        SwingUtilities.calculateInnerArea(this, innerArea);
        g.drawImage(image, 0, 0, innerArea.width, innerArea.height, this);
    }

}

I thought it should work using a Constructor as follows:

public ImageLabel(String img) {

    super();
    this.image = new ImageIcon(this.getClass().getResource(img)).getImage();
    repaint();

}

But there I have the issue, that the Label has basically no size, since it has no actual content.
When using super(" "); instead everything looks as I actually expected.

Now I am wondering - is that a legit way to approach this issue or am I missing some other (probably better) way?

È stato utile?

Soluzione

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);
    }
}

Altri suggerimenti

peeskillet already gave an appropriate answer (https://stackoverflow.com/a/21401576/3182664 ). However, in order to achieve the usual behavior (that is, to obey the general contract that is set up by the Component#setPreferredSize(Dimension) method), the getPreferredSize() method should actually be overridden like this:

@Override 
public Dimension getPreferredSize() {
    if (isPreferredSizeSet()) {
        return super.getPreferredSize();
    }
    return new Dimension(D_W, D_H);

    // Could also return this, to make the panel's 
    // preferred size match the image size
    //return new Dimension(image.getWidth(this), image.getHeight(this));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top