Problem with adjusting both transparency and color of an object

I am writing a program where when an object is picked, both it's transparency and color will be altered (the object will become opaque, and its color will turn to a predefined "selected" color), while the previously selected object will return to its original transparency and color. I am able to get this to work, except that objects that were picked two or more picks ago will remain in the selected color, albeit in their original transparency. This problem only seems to occur when transparency is factored in. The picking/color & transparency altering code is as follows.

publicclass PickingListenerextends MouseAdapter{

private PickCanvas pickCanvas;

private TextDisplay overlay;

private Primitive p;

private Primitive lastP;

private ColoringAttributes origColor;

private TransparencyAttributes origTransparency;

public PickingListener(PickCanvas pickCanvasArg, TextDisplay overlayArg){

pickCanvas = pickCanvasArg;

overlay = overlayArg;

}

publicvoid mouseClicked(MouseEvent e){

pickCanvas.setShapeLocation(e);

PickResult result = pickCanvas.pickClosest();

if (result ==null){

System.out.println("Nothing picked");

}else{

p = (Primitive) result.getNode(PickResult.PRIMITIVE);

if (p !=null){

setSelectedColor();

overlay.setPickedBarInfo((BarInformation) p.getUserData());

}

else

System.out.println("null");

}

}

privatevoid setSelectedColor(){

ColoringAttributes barCA =new ColoringAttributes();

TransparencyAttributes barTransp =new TransparencyAttributes(TransparencyAttributes.NICEST, 0.0f);

barCA.setColor(ColorConstants.SELECTED);

if (lastP !=null){

lastP.getAppearance().setColoringAttributes(origColor);

lastP.getAppearance().setTransparencyAttributes(origTransparency);

}

origColor = p.getAppearance().getColoringAttributes();

origTransparency = p.getAppearance().getTransparencyAttributes();

p.getAppearance().setColoringAttributes(barCA);

p.getAppearance().setTransparencyAttributes(barTransp);

lastP = p;

}

}

Capabilities to alter the primitive's color and transparency are all set.

(

barAppearance.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);

barAppearance.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);

barAppearance.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);barAppearance.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);

ColoringAttributes barCA =new ColoringAttributes();

barCA.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);

barCA.setCapability(ColoringAttributes.ALLOW_COLOR_READ);

)

Any insight as to why this would occur would be greatly appreciated.

-Adrian Benton

[4186 byte] By [Sorbetsa] at [2007-10-3 11:17:15]
# 1
If you are updating from a Behavior the only WakeupCriteria guaranteed to take effect in the current render frame is WakeuponElapsedFrames(0). Any updates to the scenegraph using other or no criteria will occur unpredictably. SEe the javadoc for details.regards
messengersa at 2007-7-15 13:41:36 > top of Java-index,Security,Cryptography...
# 2

I'm sorry, I'm relatively new to this. I haven't used a Behavior to initiate these changes in transparency and color, should I? If so, how would I go about that? I find it strange that if I reverse the order that I change the transparency and color in, I get the opposite problem (colors are restored correctly, but transparency is still off.) Is there a way to replace the appearance of a primitive? I've tried allowing several different capabilities:

ENABLE_APPEARANCE_MODIFY

ALLOW_APPEARANCE_WRITE

ALLOW_APPEARANCE_READ

ALLOW_APPEARANCE_OVERRIDE_WRITE

ALLOW_APPEARANCE_OVERRIDE_READ

but even with all of them allowed I keep getting a set appearance capability error when I call:

p.setAppearance(barAppearance);

I am quite stumped. Any help would be greatly appreciated.

-Adrian

Sorbetsa at 2007-7-15 13:41:36 > top of Java-index,Security,Cryptography...
# 3

A Behavior is the preferred way to make changes in a scenegraph. Here is an example from a previous post that changes the appearance of a shape when it collides with another shape.

The javadoc for javax.media.j3d.Behavior appears to refer to the issue you have.

Transparencies are confusing, different display adapters deal with them differently. Suggest you search the forum at

http://forums.java.net/jive/forum.jspa?forumID=70

for more detailed information.

regards

class CollisionDetector extends Behavior {

private static final Color3f highlightColor = new Color3f(0.0f, 1.0f, 0.0f);

private static final ColoringAttributes highlight = new ColoringAttributes(

highlightColor, ColoringAttributes.SHADE_GOURAUD);

private boolean inCollision = false;

private Shape3D shape;

private ColoringAttributes shapeColoring;

private Appearance shapeAppearance;

private WakeupOnCollisionEntry wEnter;

private WakeupOnCollisionExit wExit;

public CollisionDetector(Shape3D s) {

shape = s;

shapeAppearance = shape.getAppearance();

shapeColoring = shapeAppearance.getColoringAttributes();

inCollision = false;

}

public void initialize() {

wEnter = new WakeupOnCollisionEntry(shape);

wExit = new WakeupOnCollisionExit(shape);

wakeupOn(wEnter);

}

public void processStimulus(Enumeration criteria) {

inCollision = !inCollision;

if (inCollision) {

shapeAppearance.setColoringAttributes(highlight);

wakeupOn(wExit);

} else {

shapeAppearance.setColoringAttributes(shapeColoring);

wakeupOn(wEnter);

}

}

}

messengersa at 2007-7-15 13:41:36 > top of Java-index,Security,Cryptography...
# 4

Thanks for the help, I'll try to implement any changes to the scene graph using behaviors. Quick question: is it possible to replace the entire Apperance node of an object rather than resetting its ColoringAttributes or TransparencyAttributes? I tried to set the object to ENABLE_APPEARANCE_MODIFY, which I thought should allow reading and writing of the Appearance node, but this has still yielded a "capability not set" error. I'll try to implement a behavior to alter both the transparency and coloring of the primitive and see if that clears things up. Thanks again!

Cheers,

Adrian

Sorbetsa at 2007-7-15 13:41:36 > top of Java-index,Security,Cryptography...
# 5
Perhaps I have to construct an AlternateAppearance object to override the object's former appearance. . .
Sorbetsa at 2007-7-15 13:41:36 > top of Java-index,Security,Cryptography...