How Can I embed java3d's Canvas3d in javafx layout?

亡梦爱人 提交于 2020-01-17 03:17:13

问题


I try to embed Canvas3d in StackPane using SwingNode. I put Canvas3d into JPanel, and put JPanel into SwingNode. But only empty Jpanel was appeared. I heard in somewhere that SwingNode is rendering only pure Swing. So I used JCanvas3d instead of Canvas3d, but it is not working too! Is there any method to embed Java3d's Canvas3d in javafx layout? It's okay if not use SwingNode.

My java version is jdk 1.8_xx And Here is my source code

public class Main extends Application
{
public static void main( String[] args )
{
    launch( args );
}

@Override
public void start( Stage primaryStage )
{

    try
    {

        StackPane root = new StackPane();
        SwingNode swingNode = new SwingNode();

        root.setPrefSize( 400, 400 );
        root.setMinSize( 400, 400 );

        Scene scene = new Scene( root, 400, 400 );
        primaryStage.setScene( scene );
        primaryStage.show();
        root.setPrefSize( 400, 400 );

        root.getChildren().add( swingNode );

        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {

                GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
                Canvas3D canvas = new Canvas3D( config );

                SimpleUniverse univ = new SimpleUniverse( canvas );
                univ.getViewingPlatform().setNominalViewingTransform();
                BranchGroup scene = createSceneGraph();
                scene.compile();
                univ.addBranchGraph( scene );

                JPanel panel = new JPanel();
                panel.add( canvas );

                swingNode.setContent( panel );

            }
        } );

    }
    catch ( Exception e )
    {
        e.printStackTrace();
    }
}

public BranchGroup createSceneGraph()
{
    BranchGroup branch = new BranchGroup();

    // Make a changeable 3D transform
    TransformGroup trans = new TransformGroup();
    trans.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
    branch.addChild( trans );

    // Make a shape
    ColorCube demo = new ColorCube( 0.4 );
    trans.addChild( demo );

    // Make a behavor to spin the shape
    Alpha spinAlpha = new Alpha( -1, 4000 );
    RotationInterpolator spinner = new RotationInterpolator( spinAlpha, trans );
    spinner.setSchedulingBounds( new BoundingSphere( new Point3d(), 1000.0 ) );
    trans.addChild( spinner );

    return branch;
}
}

来源:https://stackoverflow.com/questions/35053596/how-can-i-embed-java3ds-canvas3d-in-javafx-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!