Hi,

Can anyone please help. I am working on Java 3D Cylinders. I cannot color the different faces of the Cylinder differently. TOP, BOTTOM, BODY. All should have different colors. I tried it with different appearances but to no avail.

Cylinder c=new Cylinder();
    BranchGroup contentBranch = new BranchGroup();
    Transform3D rotateCube = new Transform3D();

   Appearance ap = new Appearance();
   Appearance ap1=new Appearance();
   Appearance ap2=new Appearance();
   ap.setColoringAttributes(new ColoringAttributes(new Color3f(Color.YELLOW), ColoringAttributes.SHADE_FLAT));
   ap1.setColoringAttributes(new ColoringAttributes(new Color3f(Color.BLUE), ColoringAttributes.SHADE_FLAT));
   ap2.setColoringAttributes(new ColoringAttributes(new Color3f(Color.GREEN), ColoringAttributes.SHADE_FLAT));
   rotateCube.rotZ(12);
    //rotateCube.set(new AxisAngle4d(0, 0, 0, Math.PI / 32.0));
    TransformGroup rotationGroup = new TransformGroup(rotateCube);
    contentBranch.addChild(rotationGroup);

  //appearance.setColoringAttributes( colorAttributes );
   Shape3D sh[]=new Shape3D[3];
   sh[0]=c.getShape(Cylinder.TOP);
   sh[1]=c.getShape(Cylinder.BODY);
   sh[2]=c.getShape(Cylinder.BOTTOM);

   sh[0].setAppearance(ap);
   sh[1].setAppearance(ap1);
   sh[2].setAppearance(ap2);
   
   

    //Create the shape and add it to the branch
   rotationGroup.addChild(c);

Thanks.

Member Avatar for hfx642

Technically, you can't see the top and bottom of a (solid) cyliner at the same time.
If the cylinders are stationary (and assuming you want to see the top);
a) draw an elipse the same colour as the side
b) draw a rectangle where the bottom of the rectangle is at the midpoint (Y-Axis) of the elipse
c) draw an elipse the top colour, centred (Y-Axis) at the top of the rectangle.
If the cylinders are to be rotated (any axis), then just ignore whatever I've just told you... cause I don't know!!
Hope this helps.

Was making a slight mistake. Got it.!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.