Canvas3D does not appear in the Swing window

I am trying to insert a Canvas3D object inside a Swing JPanel, but the code does not seem to work (i.e. nothing happens):

         Canvas3D canvas = new Canvas3D (SimpleUniverse.getPreferredConfiguration ());
         SimpleUniverse universe = new SimpleUniverse (canvas);
         BranchGroup root = new BranchGroup ();
         root.addChild (new ColorCube ());
         universe.addBranchGraph (root);
         universe.getViewingPlatform (). setNominalViewingTransform ();
         canvasPanel.add (canvas);

What am I missing? JPanel was created using the NetBean Visual Editor.

+1
java swing java-3d
source share
2 answers

Perhaps you need to install a layout manager in a panel that automatically expands the child components to the full scope. By default, JPanel has a FlowLayout that does not extend child components. You can try BorderLayout instead by calling:

canvasPanel.setLayout(new BorderLayout()); 
+3
source share

Canvas3D needs the size passed to it; SimpleUniverse is not enough to configure your preferred configuration. In my case, this meant this code:

  // 3D canvas initialization
         Canvas3D canvas = new Canvas3D (SimpleUniverse.getPreferredConfiguration ());
         SimpleUniverse universe = new SimpleUniverse (canvas);
         BranchGroup root = new BranchGroup ();
         root.addChild (new ColorCube ());
         universe.addBranchGraph (root);
         universe.getViewingPlatform (). setNominalViewingTransform ();
         canvas.setSize (100, 100);
         canvasPanel.add (canvas); 
0
source share

All Articles