Skip to main content

How do I change the color of a live Shape3D object?

1 reply [Last post]
wizzer
Offline
Joined: 2006-02-14
Points: 0

I have several Shape3D objects displayed. Some are red and some are blue. I would like to change the color of one of tem to green based on selection in a tree. How do you do it?

I have tried changing the ColorAttributes in the Appearance of the Shape3D but that does not work. I have tried creating different Appearance objects with 2 different colors, but that does not work either. I have evene tried using different Materials in the different Appearances but still no luck.

Please help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
aina
Offline
Joined: 2006-05-05
Points: 0

If you have for exemple a Cylinder object, to change its color you just have to set a new appearance.

Write a method like below :

public void ModifyColorShape (Color3f coul){
Appearance app = new Appearance();
Material m = new Material();
m.setAmbientColor(new Color3f(coul));
m.setDiffuseColor(new Color3f(coul));
app.setMaterial(m);
yourCylinder.setAppearance(app);
}

Adapt it to your needs,

But before that you need to affect some capabilities to your Cylinder because it is composed by lots of Shape3Ds.

So make as below while you create your cylinder :

Enumeration en = yourCylinder.getAllChildren();
while (en.hasMoreElements()) {
Node n = (Node) en.nextElement();
if (n instanceof Shape3D) { n.setCapability(Shape3D.ALLOW_APPEARANCE_READ); n.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
}
}

It could help you !