JavaFX 2.0 の ScatterChart をシステムのクリップボードにコピーする必要があります。ScatterChart の画像全体をポッティングされたポイントでコピーする方法がよくわかりません。
2722 次
2 に答える
4
ボットがスクリーンショットを撮る必要がなくなります
/**
* 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);
}
于 2016-02-17T03:43:45.130 に答える
4
次のコードを参照してください。インポートの混乱を避けるために、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
。残念ながら、まだ十分に安定していません。
于 2011-12-18T14:03:25.170 に答える