2

this drawing area to draw image with canvas

        Canvas canvas= new Canvas();
        canvas.setHeight(500);
        canvas.setWidth(700);

GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setFill(Color.BLACK);
        gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
        gc.setFill(Color.WHITE);
        gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);

and I made this method to export drawing canvas to imagem.pgn or snapshot and its correctly

public void snapshotCanvasImageToPNG(Stage primaryStage) {

        FileChooser fileChooser = new FileChooser();

        FileChooser.ExtensionFilter extFilter
                = new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
        fileChooser.getExtensionFilters().add(extFilter);

        File file = fileChooser.showSaveDialog(primaryStage);

        if (file != null) {
            try {
                WritableImage writableImage = new WritableImage((int) canvas.getWidth(), (int) canvas.getHeight());
                canvas.snapshot(null, writableImage);
                RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
                ImageIO.write(renderedImage, "png", file);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

and I have many doubts in making these methods to open edit and save the canvas drawing to file.pgm I made this but it complicate to finish

public void openFilePGM(Stage primaryStage) {

    FileChooser fileChooser = new FileChooser();

    FileChooser.ExtensionFilter extFilter
            = new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
    fileChooser.getExtensionFilters().add(extFilter);


    File file = fileChooser.showOpenDialog(primaryStage);

    if (file != null) {
    //doubt here
    }
}

public void saveFilePGM(Stage primaryStage) {

    FileChooser fileChooser = new FileChooser();

    FileChooser.ExtensionFilter extFilter
            = new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
    fileChooser.getExtensionFilters().add(extFilter);

    File file = fileChooser.showSaveDialog(primaryStage);

    if (file != null) {
       //doubt here
    }  
}
DSanches
  • 311
  • 2
  • 3
  • 15

1 Answers1

1

The only way you will get to work with PGM images in JavaFX is through ImageJ or Java Advanced Imaging as mentioned by @VGR or another third party plugin.

Please check this plugin that contains code for such IO functions.

And you might also want to look at this answer which can show you the formats you can easily handle with JavaFX.

Community
  • 1
  • 1
Mansueli
  • 6,223
  • 8
  • 33
  • 57
  • I believe the [Java Advanced Imaging](http://www.oracle.com/technetwork/java/current-142188.html) library supports PBM/PGM/PPM. – VGR Jun 19 '14 at 21:34