Search in sources :

Example 61 with WritableImage

use of javafx.scene.image.WritableImage in project TestFX by TestFX.

the class PixelMatcherBase method match.

// ---------------------------------------------------------------------------------------------
// METHODS.
// ---------------------------------------------------------------------------------------------
@Override
public PixelMatcherResult match(Image image0, Image image1) {
    WritableImage matchImage = createEmptyMatchImage(image0, image1);
    int imageWidth = (int) matchImage.getWidth();
    int imageHeight = (int) matchImage.getHeight();
    long matchPixels = 0L;
    long totalPixels = imageWidth * imageHeight;
    for (int imageY = 0; imageY < imageHeight; imageY += 1) {
        for (int imageX = 0; imageX < imageWidth; imageX += 1) {
            Color color0 = readPixel(image0, imageX, imageY);
            Color color1 = readPixel(image1, imageX, imageY);
            boolean areColorsMatching = matchColors(color0, color1);
            if (areColorsMatching) {
                matchPixels += 1;
                Color matchColor = createMatchColor(color0, color1);
                writePixel(matchImage, imageX, imageY, matchColor);
            } else {
                Color nonMatchColor = createNonMatchColor(color0, color1);
                writePixel(matchImage, imageX, imageY, nonMatchColor);
            }
        }
    }
    return new PixelMatcherResult(matchImage, matchPixels, totalPixels);
}
Also used : WritableImage(javafx.scene.image.WritableImage) Color(javafx.scene.paint.Color) PixelMatcherResult(org.testfx.service.support.PixelMatcherResult)

Example 62 with WritableImage

use of javafx.scene.image.WritableImage in project FXGL by AlmasB.

the class MapGenerationSample method variety.

private void variety() {
    // tile size
    int size = 5;
    int W = getWidth() / size;
    int H = getHeight() / size;
    HeightMapGenerator gen = new HeightMapGenerator(2.4);
    gen.setGenFunction((nx, ny, freq) -> {
        double e = FXGLMath.noise2D(nx, ny) + 0.5 * FXGLMath.noise2D(2 * nx, 2 * ny) + 0.25 * FXGLMath.noise2D(9 * nx, 9 * ny);
        double noiseValue = Math.pow(e, 3.3);
        if (noiseValue < 0) {
            noiseValue = 0;
        }
        if (noiseValue > 1) {
            noiseValue = 1;
        }
        return noiseValue;
    });
    Grid<Double> map = gen.generate(W, H);
    map.forEach((data, x, y) -> {
        Texture texture;
        if (data < 0.2) {
            // water
            texture = new Texture(new WritableImage(size, size)).replaceColor(Color.TRANSPARENT, Color.BLUE);
        } else if (data < 0.8) {
            // grass
            texture = DSLKt.texture("grass.png", size, size);
        } else {
            // in-land grass / mud?
            texture = DSLKt.texture("grass.png", size, size).desaturate().brighter().desaturate();
        }
        Entities.builder().at(x * size, y * size).viewFromNode(texture).buildAndAttach();
    });
}
Also used : WritableImage(javafx.scene.image.WritableImage) HeightMapGenerator(com.almasb.fxgl.algorithm.procedural.HeightMapGenerator) Texture(com.almasb.fxgl.texture.Texture)

Example 63 with WritableImage

use of javafx.scene.image.WritableImage in project FXGL by AlmasB.

the class MapGenerationSample method plain.

private void plain() {
    // tile size
    int size = 5;
    int W = getWidth() / size;
    int H = getHeight() / size;
    MapGenerator<BiomeMapGenerator.BiomeData> gen = new BiomeMapGenerator(2.4);
    Grid<BiomeMapGenerator.BiomeData> map = gen.generate(W, H);
    map.forEach((data, x, y) -> {
        Texture texture;
        if (data.getElevation() < 0.2) {
            // water
            texture = new Texture(new WritableImage(size, size)).replaceColor(Color.TRANSPARENT, Color.BLUE);
        } else if (data.getElevation() < 0.8) {
            // grass
            texture = DSLKt.texture("grass.png", size, size);
        } else {
            // in-land grass / mud?
            texture = DSLKt.texture("grass.png", size, size).desaturate().brighter().desaturate();
        }
        Entities.builder().at(x * size, y * size).viewFromNode(texture).buildAndAttach();
    });
}
Also used : BiomeMapGenerator(com.almasb.fxgl.algorithm.procedural.BiomeMapGenerator) WritableImage(javafx.scene.image.WritableImage) Texture(com.almasb.fxgl.texture.Texture)

Example 64 with WritableImage

use of javafx.scene.image.WritableImage in project FXGL by AlmasB.

the class MapGenerationSample method coloredMap.

private void coloredMap() {
    // tile size
    int size = 1;
    int W = getWidth() / size;
    int H = getHeight() / size;
    HeightMapGenerator gen = new HeightMapGenerator(2.4);
    gen.setGenFunction((nx, ny, freq) -> {
        double e = FXGLMath.noise2D(nx, ny) + 0.5 * FXGLMath.noise2D(2 * nx, 2 * ny) + 0.25 * FXGLMath.noise2D(9 * nx, 9 * ny);
        double noiseValue = Math.pow(e, 3.3);
        if (noiseValue < 0) {
            noiseValue = 0;
        }
        if (noiseValue > 1) {
            noiseValue = 1;
        }
        return noiseValue;
    });
    Grid<Double> map = gen.generate(W, H);
    // use different interpolators if needed
    AnimatedColor water = new AnimatedColor(Color.DARKBLUE, Color.AQUA, Interpolators.LINEAR.EASE_OUT());
    AnimatedColor land = new AnimatedColor(Color.DARKGREEN, Color.LIGHTGREEN, Interpolators.LINEAR.EASE_OUT());
    AnimatedColor desert = new AnimatedColor(Color.YELLOW, Color.LIGHTYELLOW);
    AnimatedColor snow = new AnimatedColor(Color.BROWN, Color.WHITESMOKE);
    WritableImage image = new WritableImage(W, H);
    map.forEach((data, x, y) -> {
        AnimatedColor anim;
        if (data < 0.2) {
            anim = water;
        } else if (data < 0.4) {
            anim = land;
        } else if (data < 0.8) {
            anim = desert;
        } else {
            anim = snow;
        }
        image.getPixelWriter().setColor(x, y, anim.getValue(data));
    });
    getGameScene().addUINode(new Texture(image));
}
Also used : AnimatedColor(com.almasb.fxgl.animation.AnimatedColor) WritableImage(javafx.scene.image.WritableImage) HeightMapGenerator(com.almasb.fxgl.algorithm.procedural.HeightMapGenerator) Texture(com.almasb.fxgl.texture.Texture)

Example 65 with WritableImage

use of javafx.scene.image.WritableImage in project FXGL by AlmasB.

the class ParticleEmitter method createImage.

/**
 * Adapted from http://wecode4fun.blogspot.co.uk/2015/07/particles.html (Roland C.)
 *
 * Snapshot an image out of a node, consider transparency.
 */
private static Image createImage(Node node) {
    SnapshotParameters parameters = new SnapshotParameters();
    parameters.setFill(Color.TRANSPARENT);
    int imageWidth = (int) node.getBoundsInLocal().getWidth();
    int imageHeight = (int) node.getBoundsInLocal().getHeight();
    WritableImage image = new WritableImage(imageWidth, imageHeight);
    Async.startFX(() -> {
        node.snapshot(parameters, image);
    }).await();
    return image;
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) javafx.scene.paint(javafx.scene.paint)

Aggregations

WritableImage (javafx.scene.image.WritableImage)74 SnapshotParameters (javafx.scene.SnapshotParameters)26 PixelWriter (javafx.scene.image.PixelWriter)23 PixelReader (javafx.scene.image.PixelReader)13 IOException (java.io.IOException)12 Color (javafx.scene.paint.Color)11 BufferedImage (java.awt.image.BufferedImage)9 File (java.io.File)8 Image (javafx.scene.image.Image)7 ImageView (javafx.scene.image.ImageView)7 Point2D (javafx.geometry.Point2D)6 Group (javafx.scene.Group)5 Random (java.util.Random)4 Canvas (javafx.scene.canvas.Canvas)4 GraphicsContext (javafx.scene.canvas.GraphicsContext)4 Texture (com.almasb.fxgl.texture.Texture)3 OutputStream (java.io.OutputStream)3 Scene (javafx.scene.Scene)3 Background (javafx.scene.layout.Background)3 Scale (javafx.scene.transform.Scale)3