user3507196
user3507196

Reputation: 1

Issue with paintComponent()

public class ImagePreview extends JPanel {

    private static final long serialVersionUID = 1L;
    final float ratio = 1.0f;
    private RenderedImage image;

    public ImagePreview (int imgWidth, int imgHeight) {

        this.setPreferredSize(new Dimension((int) (ratio * imgWidth) + 5, (int) (ratio * imgHeight) + 5));
    }

    public ImagePreview (int imgWidth, int imgHeight, final RenderedImage image) {
        super();
        this.setPreferredSize(new Dimension((int) (ratio * imgWidth) + 5, (int) (ratio * imgHeight) + 5));
        this.image = image; 
        repaint();
    }

    @Override
    public synchronized void paintComponent(Graphics g) {
        super.paintComponent(g);
        VectorGraphics g2 = VectorGraphics.create(g);
        if (image != null) {
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2.drawRenderedImage(image, AffineTransform.getScaleInstance(ratio, ratio));
        }
    }
}

this method is called on a button click // call this method ImagePreview cart = new ImagePreview(imgWidth, imgHeight, image);

I am getting the image but it is not getting repaint on the Panel. I am unable in determine the reason behind that

Upvotes: 0

Views: 43

Answers (1)

camickr
camickr

Reputation: 324197

But i just want to create a preview panel for the image

When you do custom painting you need to override the getPreferredSize() method of the panel to return the size of the image, otherwise the size is (0, 0) so there is nothing to paint.

VectorGraphics g2 = VectorGraphics.create(g);

Don't post code using 3rd party classes. We don't know if the problem is with your code or the class.

If you need more help post a proper SSCCE that demonstrates the problem.

Upvotes: 1

Related Questions