2
這個繪圖區域繪製圖像用帆布打開,編輯和保存file.pgm圖像畫布的JavaFX
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);
和我做了這個方法導出繪圖畫布imagem.pgn或快照及其正確
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);
}
}
}
,我有在做這些方法可以打開編輯和保存畫布繪製file.pgm 我做了這麼多的疑慮,但它複雜化完成
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
我相信[Java Advanced Imaging](http://www.oracle.com/technetwork/java/current-142188.html)庫支持PBM/PGM/PPM。 – VGR