Search in sources :

Example 51 with WritableImage

use of javafx.scene.image.WritableImage 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)

Example 52 with WritableImage

use of javafx.scene.image.WritableImage in project honest-profiler by jvm-profiling-tools.

the class FxUtil method addColouredLabel.

// Utility methods for adding "coloured labels".
/**
 * Generates a "colored label", which is a rectangular graphic filled with the specified color, and containing the
 * specified text, and adds it to the provided {@link Pane}.
 * <p>
 * This method should be called after the provided {@link Pane} has already been added to the scene graph. The idea
 * is that with the logic below, the text gets rendered inside its proper place in the scene graph, so that CSS and
 * other style settings are automatically taken into account. The rectangle is then resized to be a bit bigger than
 * the text.
 * <p>
 * @param pane the {@link Pane} into which the graphic will be added
 * @param content the text content to be rendered in the colored label
 * @param color the background color of the label
 * @return the provided {@link Pane}
 */
public static Node addColouredLabel(Pane pane, String content, Color color) {
    StackPane stackPane = new StackPane();
    stackPane.setAlignment(CENTER);
    Text text = new Text(content);
    Rectangle rectangle = new Rectangle();
    rectangle.setFill(color);
    stackPane.getChildren().addAll(rectangle, text);
    pane.getChildren().add(stackPane);
    // Render the text, and use the resulting image size to resize the
    // rectangle
    WritableImage image = text.snapshot(null, null);
    double width = image.getWidth() + 2;
    double height = image.getHeight() + 2;
    rectangle.setWidth(max(width, height));
    rectangle.setHeight(image.getHeight() + 2);
    return pane;
}
Also used : WritableImage(javafx.scene.image.WritableImage) Rectangle(javafx.scene.shape.Rectangle) Text(javafx.scene.text.Text) StackPane(javafx.scene.layout.StackPane)

Example 53 with WritableImage

use of javafx.scene.image.WritableImage in project Zong by Xenoage.

the class JfxLayoutRenderer method paintToImage.

/**
 * Returns a {@link WritableImage} with the given page of the given {@link Layout}
 * which is rendered at the given zoom level.
 */
public static WritableImage paintToImage(Layout layout, int pageIndex, float zoom) {
    Page page = layout.getPages().get(pageIndex);
    Size2f pageSize = page.getFormat().getSize();
    int width = Units.mmToPxInt(pageSize.width, zoom);
    int height = Units.mmToPxInt(pageSize.height, zoom);
    WritableImage wim = new WritableImage(width, height);
    Canvas jfxCanvas = new Canvas(width, height);
    GraphicsContext context = jfxCanvas.getGraphicsContext2D();
    context.setFill(Color.WHITE);
    context.fillRect(0, 0, width, height);
    LayoutRenderer.paintToCanvas(layout, pageIndex, zoom, origin, new JfxCanvas(context, pageSize, CanvasFormat.Raster, CanvasDecoration.Interactive, CanvasIntegrity.Perfect));
    jfxCanvas.snapshot(null, wim);
    return wim;
}
Also used : WritableImage(javafx.scene.image.WritableImage) GraphicsContext(javafx.scene.canvas.GraphicsContext) Size2f(com.xenoage.utils.math.geom.Size2f) JfxCanvas(com.xenoage.zong.renderer.javafx.canvas.JfxCanvas) Canvas(javafx.scene.canvas.Canvas) Page(com.xenoage.zong.layout.Page) JfxCanvas(com.xenoage.zong.renderer.javafx.canvas.JfxCanvas)

Example 54 with WritableImage

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

the class ImageRescaler method rescale.

/**
 * Creates a scaled copy of the given image using nearest-neighbor scaling
 *
 * @param base
 *            The base image
 * @param hscale
 *            The horizontal scale value
 * @param vscale
 *            The vertical scale value
 * @return
 */
public static Image rescale(Image base, double hscale, double vscale) {
    int w = (int) Math.round(base.getWidth() * hscale);
    int h = (int) Math.round(base.getHeight() * vscale);
    val out = new WritableImage(w, h);
    val reader = base.getPixelReader();
    val writer = out.getPixelWriter();
    int nx, ny;
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            nx = (int) Math.floor(x / hscale);
            ny = (int) Math.floor(y / vscale);
            writer.setArgb(x, y, reader.getArgb(nx, ny));
        }
    }
    return out;
}
Also used : lombok.val(lombok.val) WritableImage(javafx.scene.image.WritableImage)

Example 55 with WritableImage

use of javafx.scene.image.WritableImage in project Krothium-Launcher by DarkLBP.

the class TexturePreview method generateLeft.

/**
 * Generates the left preview of the skin
 * @param skin The skin image
 * @param cape The cape image
 * @return The generated preview image
 */
public static Image generateLeft(Image skin, Image cape) {
    double h = skin.getHeight();
    PixelReader pr = skin.getPixelReader();
    WritableImage wi = new WritableImage(8, 32);
    PixelWriter pw = wi.getPixelWriter();
    if (h == 64) {
        // New format
        // MAIN ZONES
        // Head
        pw.setPixels(0, 0, 8, 8, pr, 16, 8);
        // Left Arm
        pw.setPixels(2, 8, 4, 12, pr, 40, 52);
        // Left Leg
        pw.setPixels(2, 20, 4, 12, pr, 24, 52);
        // EXTRA ZONES
        // Head
        renderLayer(0, 0, 8, 8, pr, pw, 48, 8);
        // Left Arm
        renderLayer(2, 8, 4, 12, pr, pw, 56, 52);
        // Left Leg
        renderLayer(2, 20, 4, 12, pr, pw, 8, 52);
    } else if (h == 32) {
        // Legacy format
        // Head
        pw.setPixels(0, 0, 8, 8, pr, 16, 8);
        // Hat
        renderLayer(0, 0, 8, 8, pr, pw, 48, 8);
        // Left Arm
        renderLayerInverse(2, 8, 4, 12, pr, pw, 40, 20);
        // Left Leg
        renderLayerInverse(2, 20, 4, 12, pr, pw, 0, 20);
    }
    if (cape != null) {
        PixelReader pr2 = cape.getPixelReader();
        pw.setPixels(6, 8, 1, 16, pr2, 0, 1);
    }
    return wi;
}
Also used : WritableImage(javafx.scene.image.WritableImage) PixelReader(javafx.scene.image.PixelReader) PixelWriter(javafx.scene.image.PixelWriter)

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