-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathScreenshotHandler.java
More file actions
70 lines (55 loc) · 1.97 KB
/
ScreenshotHandler.java
File metadata and controls
70 lines (55 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package de.piegames.blockmap.gui;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.joml.AABBd;
import javafx.concurrent.Task;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
/**
* Handles the creation of high-resolution screenshots of maps.
*/
public class ScreenshotHandler {
/**
* Take a high-resolution screenshot of a section of them map
* @param map Map to use.
* @param frustum Bounds of the image.
* @return The captured image.
*/
public static WritableImage takeScreenshot(RenderedMap map, AABBd frustum) {
double width = frustum.maxX - frustum.minX;
double height = frustum.maxY - frustum.minY;
Canvas canvas = new Canvas(width, height);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.translate(-frustum.minX, -frustum.minY);
map.draw(gc, 0, frustum, 1);
SnapshotParameters parameters = new SnapshotParameters();
parameters.setFill(Color.TRANSPARENT);
return canvas.snapshot(parameters, null);
}
/**
* Save a JavaFX image out to a file in PNG format.
*
* @param image Image to save.
* @param file File to save to.
* @throws IOException If an IO exception occurs while writing the file.
*/
public static void saveImage(Image image, File file) throws IOException {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
}
public static Task<Void> saveImageTask(Image image, File file) {
Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
saveImage(image, file);
return null;
}
};
return task;
}
}