Save the image painted by JavaFX on disk

We can save the image painted by JavaSE drive in this way:
ImageIO.write (bi BufferedImage);
Graphics g = bi.getGraphics ();
g.Draw...

In JavaFX, canvas is considered to be a node, we can use canvas.getGraphicsContext2D () to paint. But how to save us from these painted images?

Thank you.

Hello. Use Canvas.snapshot (sp, wi WritableImage SnapshotParameters);

Here is an example:

   import java.awt.image.BufferedImage;
import java.io.File;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.image.*;

import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.*;
import javax.imageio.ImageIO;
import java.io.*;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Rectangle;

public class PrintCanvas extends Application {

    @Override
    public void start(Stage primaryStage) {

        StackPane root = new StackPane();

        Scene scene = new Scene(root, 400, 450);
        WritableImage wim = new WritableImage(300, 250);

        Canvas canvas = new Canvas(300, 250);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        drawShapes(gc);
        canvas.snapshot(null, wim);
        root.getChildren().add(canvas);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();

        File file = new File("CanvasImage.png");

        try {
            ImageIO.write(SwingFXUtils.fromFXImage(wim, null), "png", file);
        } catch (Exception s) {
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    private void drawShapes(GraphicsContext gc) {
        gc.setFill(Color.GREEN);
        gc.setStroke(Color.BLUE);
        gc.setLineWidth(5);
        gc.strokeLine(40, 10, 10, 40);
        gc.fillOval(10, 60, 30, 30);
        gc.strokeOval(60, 60, 30, 30);
        gc.fillRoundRect(110, 60, 30, 30, 10, 10);
        gc.strokeRoundRect(160, 60, 30, 30, 10, 10);
        gc.fillArc(10, 110, 30, 30, 45, 240, ArcType.OPEN);
        gc.fillArc(60, 110, 30, 30, 45, 240, ArcType.CHORD);
        gc.fillArc(110, 110, 30, 30, 45, 240, ArcType.ROUND);
        gc.strokeArc(10, 160, 30, 30, 45, 240, ArcType.OPEN);
        gc.strokeArc(60, 160, 30, 30, 45, 240, ArcType.CHORD);
        gc.strokeArc(110, 160, 30, 30, 45, 240, ArcType.ROUND);
        gc.fillPolygon(new double[]{10, 40, 10, 40},
                new double[]{210, 210, 240, 240}, 4);
        gc.strokePolygon(new double[]{60, 90, 60, 90},
                new double[]{210, 210, 240, 240}, 4);
        gc.strokePolyline(new double[]{110, 140, 110, 140},
                new double[]{210, 210, 240, 240}, 4);
    }
}

Tags: Java

Similar Questions

  • I use a mac and when I try to save a file I get this message.   Unable to save the image.  Do not have enough access privileges. -5000

    I use a mac and when I try to save a file I get this message.   Unable to save the image.  Do not have enough access privileges. -5000

    local disk, I think he was trying to save it in the templates folder.  I changed it to save it on my counter top and it worked.  Rookie mistake.  This had to be the problem.

    Thank you

  • Save the Image as it goes back to the same place, even after re-booting

    After re-booting, save the image as must go to the main menu and I picked where to save the image. Currently, image tries to be registered on the same site as an earlier session. Is there a solution for this.

    Thanks jscher2000
    You said much more skillfully I did my question. I want indeed the last directory deleted to stop, so the first suggested location is the default folder, i.e. Downloads.Your suggested fix worked well and I thank you. I'm a little disappointed with Firefox that he had to go so deeply into their program for this fix. But I appreciate your expertise. See you soon.

  • Save the image does not remember last location recorded for Firefox 9

    Just updated for FF9. I'm is not just disappointed, but which are checked the the bug that causes save slot to remember where it was recorded the last file of individual sites instead of just where the last file has been saved to has not been set. Why is this? How can be addressed?

    For example, I have save a folder A Motifake image and an image of ihasahotdog to folder C. Then come back to Motifake and save a picture. Instead of folder C, save under opens to the folder instead of the folder C. I have direct save as to save the image in A file. The I'll go back to ihasahotdog, save a picture and instead of A folder, save under opens to folder C.

    This bug also occurs with all types of files, not just of images.

    The FF developers know that the "added functionality" that started with FF7 causing the FF remember the last download save the folder based on the domain in the URL instead of use the last folder saved on annoys many users both directly and by the lack of preference to toggle. Yet they will not fix it.

    Why they do that little matter how much a good idea, they think it is, most users hate it and do not want? And what is this B.S. "is under development"? They added in an updated version, why can't they get out as fast as it was added?

    Firefox 11 should be released about 12 weeks after the release of Firefox 9, so it should be in March.

  • When I right-click to save an image it won't let me save the image otherwise that as "all files".

    When I right click on an image to make a backup because it won't let me save the image in jpeg format, what is on the menu drop-down is "all files" that records under a bunch of numbers and letters, etc. It save it as an aspx file.

    Try to add the .jpg file extension yourself if Firefox does not when you save an image file.

    If you have already saved images with an incorrect file extension, then rename them and add a .jpg file extension. Check the properties of the file via the context menu to see if that worked.

  • How can I save the image of Android 2.1 on my phone without my warranty is Sub?

    Please help me save the image of Android 2.1 on my phone without my warranty is empty... His Milestone A853 I own...

    Also, can I use a custom without the guarantee ROM is Sub?

    Two of these actions could void your warranty.

  • How can you save the image to a google map that is contained in a Web site?

    How can you save the image to a google map that is contained in a Web site? I use a macbook at the beginning of 2011 pro and functional on OS X Yosemite 10.10.5. I would like to capture the image of the map and print it with a good quality.

    Take a screenshot and print that

    Google mapsHow to take a screenshot on your Mac - Apple Support

  • Save the image to a 3d graph or the face before of the vi

    Hi all

    Pluggable labview 2011, Lun VI trace a graph 3d with different measure, I would like to save the image of the 3d graph or the front directly in a file.

    Is this possible or there is t he other than the printed with screen alternatives?

    Thank you for your answers

    the output of the node is imagedata.ctl

  • How do I save the image after the converted in 64RGB

    Hello everyone,

    I'm trying to save the image of out of 64RGB on my computer. Is this possible? I had a go and could not do. Can someone look at my code please.

    Kind regards

    Lazer

    Take a look at the following:

  • VBAI 2012 "save the image calibrated to the file" issue

    Hello

    Under the State of 'Calibrate Image', 'Save the image calibrated to the file' option does not work correctly.

    With the exception of the type 'Point of Distance' of the calibration, the new image is ever recorded. I checked the calibration image .cal2 in the folder \ProgramData\National Instruments\Vision AI\Calibration Builder, each iteration the meta-data are updated but not the image.

    I have a camera calibrated with a grid (see picture) and using the model of the distortion. Working remotely can sometimes change and I need to take a recalibration. This step should be as simple as possible. Example: put a model calibration in the field of view, then press a button recalibrate.

    The problem mentioned above, it is impossible. Whenever I have to manually go into the choice of the State of the Image calibrate and update the calibration image myself. Otherwise, calibration is recalculated from the original image which is not best suited for the new working distance.

    Is there an easy way to make VBAI save a new image of calibration with each iteration?

    Or is it something that I did not understand this option 'save image calibrated to the file?

    Vision Builder for Automated Inspection VBAI 2012

    Windows 7 64 bit

    Best,

    Ken

    Hi Larpin,

    Unfortunately VBAI does not support the suggested behavior. The best solution in my opinion would be to implement your application completely in LabVIEW!

    To Eval download link. Version:

    https://lumen.NI.com/nicif/en/evallv/content.XHTML

    Kind regards

    MISIC

  • take screenshot and saves the image to a PNG file

    Hello

    How do I take screenshot and save the image as a PNG file?

    Thank you

    Avivit.

    If you want a picture of façade one VI, you can use the method to obtain images of Panel (or the Tool of Capture of Code, which offers a range of screws for this).

    If you want a picture of the entire screen, you can simulate a button on the print screen, using the method of Clipboard.Get Image Application class to get a picture of the contents of the Clipboard. If you are looking for "screenshot" you should probably find a few examples already.

  • How to save the image to an email in the mail application

    Original title: save the image in the email

    How to save a photo to an email? When I hold down the image, the only option is to 'copy '; There is no 'Save' function available.

    Hey Lucent,

    Thanks for posting your query in the Forums RT in Microsoft Windows. If I understand you correctly, you have a problem with saving an image with the mail application. I understand that it must be frustrating, but don't worry, we are here to help solve your problem and guide you in the right direction.

    In the Mail application, you wouldn't be able to do differently than the option to right click functions. The dialog box you get with the "save image under" comes to a browser. If you use Internet Explorer to check email, you'll get the option to click with the right button on a photo. In the Mail app if you want to download an image that was attached to the email, you can click on the picture to download and click it a second time to get the options to open or save.

    You can also check the suggestions provided by Jianan! responded on October 31, 2012 in the following link:

    http://answers.Microsoft.com/en-us/Windows/Forum/windows_rt-ECOMS/how-do-i-save-a-email-picture-attachment/ef0dcd13-D219-4864-88b6-6bf5e764ca8f

    Please answer us on the State of the question to help you further.

  • Lightroom will stop when you try to save the image to the Gallery

    Lightroom will stop when you try to save the image to the gallery. It just stops without warning. And he always seems to be synchronization?

    I have a Huawei p7 Please help!

    Hi AnnCicilie,

    Make sure that if the application is up to date.

    If this does not help, could you please share the newspapers of diagnosis by PM so that we can check more?

    Follow these steps to collect logs:

    • Open Mobile with Lightroom application
    • Click on the LR icon at the top left:

    • Press and hold the Lr icon at the top left that mentions your Adobe ID and click on generate Diagnostic log

    • Send email to yourself so that you have them on the desktop and then, share with me by PM

    Concerning

    Claes

  • How to create a Viewer without buying the image? I want to be able to save the images I'm looking a subject without purchase or download.

    How to create a Viewer without buying the image? I want to be able to save the images I'm looking a subject without purchase or download.

    After talking to the Customer Service, I now get it. Thank you.

    That is to say: down arrow recorded a watermark image in the gallery without you buy. You can download an overview lores on your desktop for motorhomes with this same arrow, but it is unfortunately a watermark (unlike the DPC). You can also copy and send a link to the Gallery clients to review before you buy.

  • Why you need to save the images in photomerge panorama?

    Hello.

    I don't understand why with the new version of the Photomerge panorama when I select "create panorama" the program asks me to save the images prior to their merger.

    In addition I don' t understand what foto I must record and in which format?  (all the? in JPG? in PSD?)

    Even after doing this (Save ALL images to merge as a JPG-copy) I can't go forward.

    Thank you to anyone to help me.

    Luigi

    luigig25855901 wrote:

    After my post, I read that you need to open the images, export them (with mutiple of edit function) to a separate folder, and then from the p.panorama with the 'new' images. It worked, but I guess why it can't be fixed by Adobe. It is an unnecessary extra work.

    Luigi

    See this other post.

    Panoramas - Error Message: the Documents must be registered before they can be merged

    The problem only arises with the images in a vertical position.

    It didn't happen with older versions of items, so I don't think that it can not be solved.

    You don't have any extra work if you work from the organizer and you export your files to the correct resolution.

Maybe you are looking for

  • Is it possible to use flash video (canon t6i)

    Is it possible to use the built in flash during video capture? If yes than how? (for canon t6i)

  • Problem of earthing GPIB-USB-HS connecting to a laptop?

    Hello The installation guide for the GPIB-USB-HS asks to "ensure that the computer and the GPIB devices are at the same potential on the ground."  I was wondering if there could be problems with the connection of portable computers (because they floa

  • Windows Firewall/ICS will not start.

    Work on my aunts old Dell Dimension 3000 with XP Home Edition. She had downloaded about 5 to 6 cents free games and the 75g hard drive was full of 97% and I had to boot into safe mode and to sit and to uninstall programs for about 3 hours just to mak

  • I can't answer or send e-mails.

    After that a virus has left my computer does not work, I had to have my hard drive erased professionally.  By creating a new Windows Mail account (I used Outlook Express). I can receive emails, but cannot send or reply to emails.  I called my interne

  • Update 2.3.3 (all THE KITS) x10a and x10i

    Update 2.3.3 (all THE KITS) x10a and x10i http://www.topsony.com/Forum/local_links.php?CATID=4556 Download FlashTool http://Forum.xda-developers.com/showthread.php?t=920746 or http://Androxyde.github.com/Flashtool/