make JavaFX Image Transparency - Java JavaFX

Java examples for JavaFX:Image

Description

make JavaFX Image Transparency

Demo Code


import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.PixelWriter;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

public class Main{
    private static final Logger log = Logger.getLogger(JFXUtils.class
            .getName());//  ww w.jav  a 2s  .c o m
    
    public static Image makeTransparency(Image image, int argb) {
        log.log(Level.FINEST, "writable="
                + image.getPixelReader().getPixelFormat().isWritable()
                + ", class=" + image.getClass().getName());
        final int width = (int) image.getWidth();
        final int height = (int) image.getHeight();
        final PixelReader reader = image.getPixelReader();
        final WritableImage wi = new WritableImage(reader, width, height);
        final PixelWriter writer = wi.getPixelWriter();
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                //            System.out.print(reader.getArgb(x, y));
                if (reader.getArgb(x, y) == argb) {
                    writer.setArgb(x, y, 0);
                }
            }
        }
        //      System.out.println();
        return wi;
    }
}

Related Tutorials