gpt4 book ai didi

java - 如何将滚动 Pane 内容保存为 jpg 文件

转载 作者:行者123 更新时间:2023-11-30 02:34:47 25 4
gpt4 key购买 nike

下面是我的代码,我尝试将 ScrollPane 内容保存为 jpg 文件。它给了我一个 IllegalArgumentException 错误。到目前为止我已经尝试过 thisthis ,但显然我将错误的参数放入 ImageIO.write 方法中。我在这里缺少什么?

    package urd.View;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;

/**
*
* @author urd
*/
public class NewFXMain extends Application {

private final int width = 250;
private final int height = 250;

private final int radius = 15;
double circleX = width / 2;
double circleY = height / 2;
Circle circle = new Circle(circleX, circleY, radius);

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

/**
* @param primaryStage the command line arguments
*/
@Override
public void start(Stage primaryStage) {
BorderPane rootPane = new BorderPane();
ScrollPane scrollPane = new ScrollPane();
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
Menu generator = new Menu("generate");
MenuItem generateItem = new MenuItem("generate");
menuBar.getMenus().addAll(fileMenu, generator);
MenuItem saveItem = new MenuItem("Save Image");
fileMenu.getItems().addAll(saveItem);
generator.getItems().addAll(generateItem);
rootPane.setTop(menuBar);
rootPane.setCenter(scrollPane);
Scene scene = new Scene(rootPane, 400, 350);
primaryStage.setTitle("Image Generator");
primaryStage.setScene(scene);
primaryStage.show();
saveItem.setOnAction((ActionEvent event) -> {
// the content of scrollPane is saved as a JPEG file.
WritableImage img = scrollPane.snapshot(new SnapshotParameters(), null);
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
File fileToSave = chooser.getSelectedFile();
BufferedImage img2 = SwingFXUtils.fromFXImage(img, null);
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
ImageIO.write(img2, "png", fileToSave);
} catch (IOException ex) {
Logger.getLogger(GuiClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
generateItem.setOnAction((ActionEvent event) -> {
//Here we call the generateImage method which takes in two int values and returns a pane
scrollPane.setContent(generateImage());
});
}

private Pane generateImage() {
Rectangle rectCanvas = new Rectangle(width,
height);
rectCanvas.setFill(Color.GREEN);
circle.setFill(Color.RED);
Pane pane = new Pane();
pane.getChildren().addAll(rectCanvas, circle);
return pane;
}

}

这是IllegalArgumentException的日志

    Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: output == null!
at javax.imageio.ImageIO.write(ImageIO.java:1516)
at urd.View.NewFXMain.lambda$start$0(NewFXMain.java:86)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.control.MenuItem.fire(MenuItem.java:462)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.doSelect(ContextMenuContent.java:1405)
at com.sun.javafx.scene.control.skin.ContextMenuContent$MenuItemContainer.lambda$createChildren$343(ContextMenuContent.java:1358)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

最佳答案

chooser.getSelectedFile() 在调用 showSaveDialog() 后返回 File 对象,因此在调用 showSaveDialog() 方法后创建文件对象,如下所示。

    saveItem.setOnAction((ActionEvent event) -> {
// the content of scrollPane is saved as a JPEG file.
WritableImage img = scrollPane.snapshot(new SnapshotParameters(), null);
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
// File fileToSave = chooser.getSelectedFile();//Remove this line.
BufferedImage img2 = SwingFXUtils.fromFXImage(img, null);
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File fileToSave = chooser.getSelectedFile();
ImageIO.write(img2, "png", fileToSave);
} catch (IOException ex) {
Logger.getLogger(GuiClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

关于java - 如何将滚动 Pane 内容保存为 jpg 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43378085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com