I am trying to draw an image in a JavaFX Canvas
. However, the conventional drawImage()
method of GraphicsContext
seems to produce some sort of blurred or aliased results. Probably because I am using a Retina MacBook Pro.
I found a solution here: https://stackoverflow.com/a/26706028/555690
public class ImageRenderer {
public void render(GraphicsContext context, Image image, int sx, int sy, int sw, int sh, int tx, int ty) {
PixelReader reader = image.getPixelReader();
PixelWriter writer = context.getPixelWriter();
for (int x = 0; x < sw; x++) {
for (int y = 0; y < sh; y++) {
Color color = reader.getColor(sx + x, sy + y);
if (color.isOpaque()) {
writer.setColor(tx + x, ty + y, color);
}
}
}
}
}
It works, but sometimes I need to draw the target rect with a particular size (so I need to add parameters for int tw
and int th
), but I don't know how to adjust the logic of the above method to make use of them.
How can I draw images into JavaFX Canvas
without blur/aliasing in a given area?