在 JavaFX 2.0 中将 ScatterChart 的图像复制到系统剪贴板

发布于 2024-12-21 02:14:00 字数 71 浏览 1 评论 0原文

我需要将 JavaFX 2.0 中的 ScatterChart 复制到系统剪贴板。我不太确定如何使用盆栽点复制散点图的整个图像。

I need to copy a ScatterChart in JavaFX 2.0 to the system clipboard. I'm not really sure how to copy the whole image of the ScatterChart with the potted points.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看透却不说透 2024-12-28 02:14:00

不再需要任何机器人来截取屏幕截图

/**
 * Sets the image content of the clipboard to the chart supplied
 * @param chart chart you wish to copy to the clipboard
 */
public void copyChartToClipboard(ScatterChart<Double, Double> chart) {
    WritableImage image = chart.snapshot(new SnapshotParameters(), null);
    ClipboardContent cc = new ClipboardContent();
    cc.putImage(image);
    Clipboard.getSystemClipboard().setContent(cc);
}

Gets rid of the need for any bots to take screenshots

/**
 * Sets the image content of the clipboard to the chart supplied
 * @param chart chart you wish to copy to the clipboard
 */
public void copyChartToClipboard(ScatterChart<Double, Double> chart) {
    WritableImage image = chart.snapshot(new SnapshotParameters(), null);
    ClipboardContent cc = new ClipboardContent();
    cc.putImage(image);
    Clipboard.getSystemClipboard().setContent(cc);
}
故人的歌 2024-12-28 02:14:00

请参阅下一段代码。我为所有非 javafx 类添加了完整的包名称,以避免导入混乱。

public void start(final Stage primaryStage) throws Exception {
    VBox root = new VBox();
    final Scene scene;
    primaryStage.setScene(scene = new Scene(root));
    NumberAxis xAxis = new NumberAxis("X-Axis", 0d, 8.0d, 1.0d);
    NumberAxis yAxis = new NumberAxis("Y-Axis", 0.0d, 5.0d, 1.0d);
    ObservableList<XYChart.Series> data = FXCollections.observableArrayList(
            new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList(
            new XYChart.Data(0.2, 3.5),
            new XYChart.Data(0.7, 4.6),
            new XYChart.Data(7.8, 4.0))));
    final ScatterChart chart = new ScatterChart(xAxis, yAxis, data);

    Button btnShoot = new Button("screenshot");
    btnShoot.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent t) {
            try {
                // getting screen coordinates
                Bounds b = chart.getBoundsInParent();
                int x = (int)Math.round(primaryStage.getX() + scene.getX() + b.getMinX());
                int y = (int)Math.round(primaryStage.getY() + scene.getY() + b.getMinY());
                int w = (int)Math.round(b.getWidth());
                int h = (int)Math.round(b.getHeight());
                // using ATW robot to get image
                java.awt.Robot robot = new java.awt.Robot();
                java.awt.image.BufferedImage bi = robot.createScreenCapture(new java.awt.Rectangle(x, y, w, h));
                // convert BufferedImage to javafx.scene.image.Image
                java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
                ImageIO.write(bi, "png", stream);
                Image image = new Image(new java.io.ByteArrayInputStream(stream.toByteArray()), w, h, true, true);
                // put it to clipboard
                ClipboardContent cc = new ClipboardContent();
                cc.putImage(image);
                Clipboard.getSystemClipboard().setContent(cc);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    });
    root.getChildren().addAll(chart, btnShoot);
    primaryStage.show();
}

注意:此方法涉及将 AWT 与 JavaFX 并行使用,这通常不是一个好主意,并且可能不适用于所有配置。最好使用 GlassRobot 而不是 AWTRobot。不幸的是它还不够稳定。

See next piece of code. I've added full package names for all non-javafx classes to avoid imports mess.

public void start(final Stage primaryStage) throws Exception {
    VBox root = new VBox();
    final Scene scene;
    primaryStage.setScene(scene = new Scene(root));
    NumberAxis xAxis = new NumberAxis("X-Axis", 0d, 8.0d, 1.0d);
    NumberAxis yAxis = new NumberAxis("Y-Axis", 0.0d, 5.0d, 1.0d);
    ObservableList<XYChart.Series> data = FXCollections.observableArrayList(
            new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList(
            new XYChart.Data(0.2, 3.5),
            new XYChart.Data(0.7, 4.6),
            new XYChart.Data(7.8, 4.0))));
    final ScatterChart chart = new ScatterChart(xAxis, yAxis, data);

    Button btnShoot = new Button("screenshot");
    btnShoot.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent t) {
            try {
                // getting screen coordinates
                Bounds b = chart.getBoundsInParent();
                int x = (int)Math.round(primaryStage.getX() + scene.getX() + b.getMinX());
                int y = (int)Math.round(primaryStage.getY() + scene.getY() + b.getMinY());
                int w = (int)Math.round(b.getWidth());
                int h = (int)Math.round(b.getHeight());
                // using ATW robot to get image
                java.awt.Robot robot = new java.awt.Robot();
                java.awt.image.BufferedImage bi = robot.createScreenCapture(new java.awt.Rectangle(x, y, w, h));
                // convert BufferedImage to javafx.scene.image.Image
                java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
                ImageIO.write(bi, "png", stream);
                Image image = new Image(new java.io.ByteArrayInputStream(stream.toByteArray()), w, h, true, true);
                // put it to clipboard
                ClipboardContent cc = new ClipboardContent();
                cc.putImage(image);
                Clipboard.getSystemClipboard().setContent(cc);
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    });
    root.getChildren().addAll(chart, btnShoot);
    primaryStage.show();
}

N.B.: this approach involves using AWT side-by-side with JavaFX which is generally not a good idea and may not work on all configuration. It's better to use GlassRobot instead of AWTRobot. Unfortunately it's not stable enough yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文