Search code examples
canvasbackgroundjava-3d

How to set Canvas3D background color?


I have an application with a Canvas3D class for drawing three-dimensional objects. The canvas background is black by default. I want to change it but I don't know how. I have found a piece of code from internet but it doesn't work. Here is the code:

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


    JPanel centerPanel = new JPanel(new GridLayout(1, 1));
    centerPanel.setBorder(BorderFactory.createLineBorder(Color.black));
    centerPanel.add(canvas);

    universe = new SimpleUniverse(canvas);

    Background newBk = new Background(new Color3f(Color.WHITE)); 
    newBk.setApplicationBounds(new BoundingSphere(new Point3d(0f,0f,0f), 100)); 
    canvas.getGraphicsContext3D().setBackground(newBk);

Would you plese helpm with this problem?

Thanks in advance.


Solution

  • Background background = new Background(new Color3f(1f,0,0));
    BoundingSphere sphere = new BoundingSphere(new Point3d(0,0,0), 100000);
    background.setApplicationBounds(sphere);
    viewingPlatform.addChild(background);