gpt4 book ai didi

java - 如何在 JavaFX 中嵌入 Piccolo 2D Canvas ?

转载 作者:行者123 更新时间:2023-11-29 08:56:21 24 4
gpt4 key购买 nike

如何在 JavaFX 中嵌入 Piccolo2D Canvas ?

我认为它应该通过 SwingNode 工作, 因为 Piccolo 有一个名为 PCanvasSwing 控件.

这种方法适用于 Swing:

public static void main(String[] args) {

PPath ellipse = PPath.createEllipse(100,100,400,200);

PCanvas canvas = new PCanvas();
canvas.getLayer().addChild(ellipse);


JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.add(canvas, BorderLayout.CENTER);


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocation(0, 0);
frame.setTitle("PCanvas_Try01");
frame.setVisible(true);
}

但这不适用于 JavaFX:

@Override
public void start(Stage stage) throws Exception {

PPath ellipse = PPath.createEllipse(100, 100, 400, 200);

PCanvas canvas = new PCanvas();
canvas.getLayer().addChild(ellipse);

SwingNode swingNode = new SwingNode();
swingNode.setContent(canvas);

Group group = new Group();
group.getChildren().add(swingNode);

Scene scene = new Scene(group);

stage.setTitle("PCanvas_Try02");
stage.setScene(scene);
stage.show();

}

最佳答案

Group 可能存在一些问题。以下似乎有效:

import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.PCanvas;
import javafx.application.Application;
import javafx.embed.swing.SwingNode;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class DemoPiccoloFx extends Application {

@Override
public void start(Stage stage) {
PPath ellipse = PPath.createEllipse(100, 100, 400, 200);

PCanvas canvas = new PCanvas();
canvas.getLayer().addChild(ellipse);

SwingNode swingNode = new SwingNode();
swingNode.setContent(canvas);

StackPane pane = new StackPane();
pane.getChildren().add(swingNode);
stage.setScene(new Scene(pane, 500, 300));
stage.setTitle("PCanvas_Try02");
stage.show();
}

public static void main(String[] args) {
launch(args);
}
}

enter image description here

关于java - 如何在 JavaFX 中嵌入 Piccolo 2D Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20141385/

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