Save buffered image with transparent background

You are setting the Buffered Image have a type of just RGB which has no Alpha component, you will have to use one that does have alpha to maintain transparency.


ready to use end to end example

it will create png picture with transparency and 2 x rectangles

compilation time - 2019_04_10__00_12_03_236

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

// ready to use end to end example
// it will create png picture with transparency and 2 x rectangles
// compilation time - 2019_04_10__00_12_03_236
public class java_create_png_image_with_transparency_end_to_end_example {

    public static void main(String[] args) throws IOException {
        Path outPath = Paths.get("C:\\_tmp_out_\\");
        if (!Files.exists(outPath)) {
            Files.createDirectory(outPath);
        }

        String timeNow = DateTimeFormatter
                .ofPattern("yyyy_MM_dd__HH_mm_ss_SSS")
                .format(LocalDateTime.now());
        String filename = "test_png_pic__" + timeNow + "__.png";
        File absOutFile = outPath.resolve(filename).toFile();

        int width = 300;
        int height = 300;

        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.setComposite(AlphaComposite.Clear);
        g2d.fillRect(0, 0, width, height);

        g2d.setComposite(AlphaComposite.Src);
        int alpha = 127; // 50% transparent
        g2d.setColor(new Color(255, 100, 100, alpha));
        g2d.fillRect(100, 100, 123, 123);

        g2d.setColor(new Color(0, 0, 0));
        g2d.fillRect(30, 30, 60, 60);

        g2d.dispose();

        ImageIO.write(bufferedImage, "png", absOutFile);
        System.out.println("File saved to:");
        System.out.println(absOutFile);
    }
}

Use BufferedImage.TYPE_INT_ARGB instead of BufferedImage.TYPE_INT_RGB. And save it to PNG image, JPEG does not support the transparency.

UPD:

For set the background transparent, use it:

g2d.setComposite(AlphaComposite.Clear);
g2d.fillRect(0, 0, w, h);

And for draw your image:

g2d.setComposite(AlphaComposite.Src);
drawPoints(Tablet.getPenPoints(), g2d, Color.BLACK);