2013-11-22 72 views
2

如何嵌入Piccolo2D畫布內JavaFX如何在JavaFX中嵌入Piccolo2D畫布?

我認爲應該通過SwingNode工作,因爲Piccolo有一個名爲PCanvasSwing控制。

這種方法適用於鞦韆:

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(); 

    } 

回答

0

可能會有一些問題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

+0

你爲什麼要使用PSwingCanvas?它的目的看起來相反! –

+0

@SuzanCioc是的,正確的,在這種情況下,無需使用'PCanvas'就能正常工作。但是如果你添加'PSwing'節點,那麼應該有'PSwingCanvas'。我更新了答案並將其刪除。 – tenorsax