javaeclipseswtbufferedimagewindowbuilder

How to display a PNG image in a java SWT?


I need to display a PNG image in a SWT Java window, I'm using WindowBuilder and Eclipse.

First I tried with a label and this code:

Label lblNewLabel = new Label(this, SWT.NONE);
        lblNewLabel.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true, 1, 1));
        Image image = new Image(display, "img/selae_mini.png");     
        lblNewLabel.setImage(image)

It worked when executing in eclipse, but when I generate the jar, then, it doesn't work. Then I found on Stack Overflow that you must use ClassLoader.getSystemClassLoader().getResourceAsStream to get a bufferedImage and after that you must convert that bufferedImage to a ImageData, and finally convert that into a SWT Image.

So I tried with this code:

protected Image readImage(String path, Display display) {
    InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
    BufferedImage bi = null;
    try {
        bi = ImageIO.read(stream);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    return new Image(display, convertToSWT(bi));
}

public static ImageData convertToSWT(BufferedImage bufferedImage) {
    if (bufferedImage.getColorModel() instanceof DirectColorModel) {
        DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
        PaletteData palette = new PaletteData(
            colorModel.getRedMask(),
            colorModel.getGreenMask(),
            colorModel.getBlueMask()
        );
        ImageData data = new ImageData(
            bufferedImage.getWidth(),
            bufferedImage.getHeight(), colorModel.getPixelSize(),
            palette
        );
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[3];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                int pixel = palette.getPixel(
                    new RGB(pixelArray[0], pixelArray[1], pixelArray[2])
                );
                data.setPixel(x, y, pixel);
            }
        }
        return data;
    } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
        IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
        int size = colorModel.getMapSize();
        byte[] reds = new byte[size];
        byte[] greens = new byte[size];
        byte[] blues = new byte[size];
        colorModel.getReds(reds);
        colorModel.getGreens(greens);
        colorModel.getBlues(blues);
        RGB[] rgbs = new RGB[size];
        for (int i = 0; i < rgbs.length; i++) {
            rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
        }
        PaletteData palette = new PaletteData(rgbs);
        ImageData data = new ImageData(
            bufferedImage.getWidth(),
            bufferedImage.getHeight(),
            colorModel.getPixelSize(),
            palette
        );
        data.transparentPixel = colorModel.getTransparentPixel();
        WritableRaster raster = bufferedImage.getRaster();
        int[] pixelArray = new int[1];
        for (int y = 0; y < data.height; y++) {
            for (int x = 0; x < data.width; x++) {
                raster.getPixel(x, y, pixelArray);
                data.setPixel(x, y, pixelArray[0]);
            }
        }
        return data;
    }
    return null;
}

The problem now is that my Image is a PNG file, and when doing the IF of the convertToSWT method, it gets that the image has a ColorModel called #pixelBits, so it returns null on that method! and I can't find any info about how to solve this problem.


Solution

  • I had this problem in before, and I resolved that. assuming your image has located in the resource folder of your project:

    String yourImg = "sampleImg.png";
    ...
    Label swtImg = new Label(composite, SWT.NONE);
    swtImg.setImage(new Image(display, YourClassName.class.getResourceAsStream(yourImg)));
    

    it's worked for me! good luck ;)