Hi!

I am working on scaling the image that is placed in JLabel. The scaling method itself works more or less fine, however a black border appears around the image after scaling. I tried to make the transparent/white JLabel background, but my attempts failed. Please explain me how could I avoid this black border? Thanks!

private void doZoom(int scaleFactor) {
        Component[] c = MainClass.getSelectablePanel().getComponents();
        for(int i = 0; i < c.length; i++) {
            if(c[i] instanceof MyLabel) {
                MyLabel selectedLabel = (MyLabel)c[i];
                BufferedImage source = (BufferedImage) selectedLabel.getOriginalImage();
                int w = selectedLabel.getWidth() + scaleFactor;
                int h = selectedLabel.getHeight() + scaleFactor;
                BufferedImage result = copyImage(source, w, h);
                selectedLabel.setSize(new Dimension(w, h));
                selectedLabel.setIcon(scale(result, w, h));
            }
        }
        repaint();
    }

    public static BufferedImage copyImage(BufferedImage image, int width, int height) {
        int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
        BufferedImage copiedImage = new BufferedImage(width, height, type);
        Graphics2D g = copiedImage.createGraphics();
        g.setComposite(AlphaComposite.Src);

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);

        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return copiedImage;
    }

    private ImageIcon scale(Image src, int w, int h) {
        int type = BufferedImage.TYPE_INT_RGB;
        BufferedImage dst = new BufferedImage(w, h, type);
        Graphics2D g2 = dst.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(src, 0, 0, w, h, this);
        g2.dispose();
        return new ImageIcon(dst);
    }
public class MyLabel extends JLabel {
    private ImageIcon imageicon;
    private Image img;

    public MyLabel(ImageIcon ii) {
        this.imageicon = ii;
        this.setIcon(ii);
    }

    public ImageIcon getOriginalIcon() {
        return imageicon;
    }

    public void setImage(Image img) {
        this.img = img;
    }

    public Image getOriginalImage() {
        return img;
    }

  }

Recommended Answers

All 3 Replies

Can you please attach the image to this Thread. It will get more clearer

Ok, attached you can find an image before and after scaling. So, you may see a black border around the image in second picture, which I need to clean.

It was caused by the alpha-channel. I changed the code as follows to make it workable:

public static BufferedImage copyImage(BufferedImage image, int width, int height) {
        int type = BufferedImage.TYPE_INT_ARGB;
        BufferedImage copiedImage = new BufferedImage(width, height, type);
...
}

    private ImageIcon scale(Image src, int w, int h) {
        int type = BufferedImage.TYPE_INT_ARGB;
        BufferedImage dst = new BufferedImage(w, h, type);
...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.