transform JavaFX Image - Java JavaFX

Java examples for JavaFX:Image

Description

transform JavaFX Image

Demo Code


import java.util.function.UnaryOperator;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;

public class Main{
    public static Image transform(Image in, UnaryOperator<Color> f) {

        int width = (int) in.getWidth();
        int height = (int) in.getHeight();
        WritableImage out = new WritableImage(width, height);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                out.getPixelWriter().setColor(x, y,
                        f.apply(in.getPixelReader().getColor(x, y)));
            }//from   w  w  w.  j a v a2 s  .c o  m
        }

        return out;
    }
    public static Image transform(Image in, ColorTransformer f) {

        int width = (int) in.getWidth();
        int height = (int) in.getHeight();
        WritableImage out = new WritableImage(width, height);

        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                out.getPixelWriter().setColor(x, y,
                        f.apply(x, y, in.getPixelReader().getColor(x, y)));
            }
        }

        return out;
    }
}

Related Tutorials