|
You can use getUserData() and setUserData(Object o). It can contain any object so a String containing the name can be used. Here is a piece of code I use that gets the names and positions of BranchGroups and TransformGroups. The first piece of code gets all the Groups (pass it the scenegraph and 0 as initial values). The second piece of code produces the Strings:
/** * This method traverses the tree and returns the positions. * * @return The String representation of the tree. */
public String traversePositions(Group tfg, int level) { Transform3D tempTrans = new Transform3D(); Vector3f vecTemp = new Vector3f(); Enumeration enum; String output = ""; int localLevel = level;
try { enum = tfg.getAllChildren(); level++;
while(enum.hasMoreElements()) { try { Group tfg1 = (Group)enum.nextElement(); output += traversePositions(tfg1, level); try { TransformGroup tfg2 = (TransformGroup)tfg; tfgCount++; ((TransformGroup)tfg).getTransform(tempTrans); tempTrans.get(vecTemp); }
catch (Exception e)// TransformGroupCastException { brgCount++; }
}
catch (Exception e)// GroupCastException { othCount++; }
}//end while loop
}
catch(Exception e)// All other exceptions {}
output += createPosInfo(tfg, localLevel);
return output; }
/** * This method creates a String containing the name and position of * a transform group. * * @param tg The transform group. * @param name The name of the transfrom group. * @param level The level of the transform group (root = 0). * @return The String representation. */
public String createPosInfo(Group tg, int level) { Transform3D tempTrans = new Transform3D(); Vector3f tempVec = new Vector3f(); Vector3f tempVec2 = new Vector3f(); String tabs = ""; for (int i = 0; i < level; i++) tabs += "\t"; if ((String)tg.getUserData() != null) { try { tg.getLocalToVworld(tempTrans); tempTrans.get(tempVec2); ((TransformGroup)tg).getTransform(tempTrans); tempTrans.get(tempVec); tempVec2.add(tempVec); tabs += " " + (String)tg.getUserData() + tempVec2.toString() + "\n"; }
catch (Exception e) { tabs += " " + (String)tg.getUserData() + "\n"; } }
return tabs; }
Hope this helps. Rich Bone Birmingham University |
- [JAVA3D] basic picking Sandegren, Eric S.
- Richard Bone
