Search in sources :

Example 1 with FxGraphics2DOutput

use of org.graphstream.ui.fx_viewer.util.FxGraphics2DOutput in project gs-ui-javafx by graphstream.

the class FxGraphRenderer method screenshot.

public void screenshot(String filename, int width, int height) {
    if (graph != null) {
        if (filename.endsWith("png") || filename.endsWith("PNG")) {
            WritableImage wim = new WritableImage(width, height);
            Canvas canvas = new Canvas(width, height);
            GraphicsContext c = canvas.getGraphicsContext2D();
            render(c, 0, 0, width, height);
            SnapshotParameters sp = new SnapshotParameters();
            sp.setFill(Color.TRANSPARENT);
            canvas.snapshot(sp, wim);
            File file = new File(filename);
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (filename.endsWith("bmp") || filename.endsWith("BMP")) {
            WritableImage wim = new WritableImage(width, height);
            Canvas canvas = new Canvas(width, height);
            GraphicsContext c = canvas.getGraphicsContext2D();
            render(c, 0, 0, width, height);
            SnapshotParameters sp = new SnapshotParameters();
            sp.setFill(Color.TRANSPARENT);
            canvas.snapshot(sp, wim);
            File file = new File(filename);
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "bmp", file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (filename.endsWith("jpg") || filename.endsWith("JPG") || filename.endsWith("jpeg") || filename.endsWith("JPEG")) {
            WritableImage wim = new WritableImage(width, height);
            Canvas canvas = new Canvas(width, height);
            GraphicsContext c = canvas.getGraphicsContext2D();
            render(c, 0, 0, width, height);
            SnapshotParameters sp = new SnapshotParameters();
            sp.setFill(Color.TRANSPARENT);
            canvas.snapshot(sp, wim);
            File file = new File(filename);
            try {
                ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "jpg", file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (filename.toLowerCase().endsWith("svg")) {
            try {
                String plugin = "org.graphstream.ui.batik.BatikGraphics2D";
                Class<?> c = Class.forName(plugin);
                Object o = c.newInstance();
                if (o instanceof FxGraphics2DOutput) {
                    FxGraphics2DOutput out = (FxGraphics2DOutput) o;
                    GraphicsContext g2 = out.getGraphics();
                    render(g2, (int) camera.getMetrics().viewport[0], (int) camera.getMetrics().viewport[1], (int) camera.getMetrics().viewport[2], (int) camera.getMetrics().viewport[3]);
                    out.outputTo(filename);
                } else {
                    System.err.println(String.format("Plugin %s is not an instance of Graphics2DOutput (%s).", plugin, o.getClass().getName()));
                }
            } catch (Exception e) {
                System.err.println("Unexpected error during screen shot. " + e);
            }
        }
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) GraphicsContext(javafx.scene.canvas.GraphicsContext) Canvas(javafx.scene.canvas.Canvas) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) FxGraphics2DOutput(org.graphstream.ui.fx_viewer.util.FxGraphics2DOutput)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 SnapshotParameters (javafx.scene.SnapshotParameters)1 Canvas (javafx.scene.canvas.Canvas)1 GraphicsContext (javafx.scene.canvas.GraphicsContext)1 WritableImage (javafx.scene.image.WritableImage)1 FxGraphics2DOutput (org.graphstream.ui.fx_viewer.util.FxGraphics2DOutput)1