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);
}
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();
});
}
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();
});
}
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));
}
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;
}
Aggregations