Search code examples
javacolorsjava-3d

Changing color of shape Java3D


I am just trying to change the colour of a cylinder I create with Java3D to yellow instead of the standard grey/black. My code seems correct however the shape remains black the whole time, here it is:

protected BranchGroup createSphere(/*Color color*/) {
        BranchGroup bg = new BranchGroup();
        bg.setCapability(BranchGroup.ALLOW_DETACH);

        Appearance app = new Appearance();
        Color3f color = new Color3f(Color.yellow);
        Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f white = new Color3f(1.0f, 1.0f, 1.0f);

        app.setMaterial(new Material(color, black, color, white, 70f));

        bg.addChild(new com.sun.j3d.utils.geometry.Cylinder());
        bg.setUserData("Sphere");
        return bg;
    }

Solution

  • You never apply your appearance to the Cylinder

    Cylinder myCylinder = new com.sun.j3d.utils.geometry.Cylinder();
    myCylinder.setAppearance(app);
    bg.addChild(myCylinder);