adding children at runtime?
Dear all,
I'm developing an applet using java3D. the basic idea is to display spheres on the canvas and when a user picks a sphere, it is translated to the origin and a different set of spheres is displayed. i could add children to my BranchGroup at runtime, but could not display them properly. the children which dont have any kind of transform applied on them are displayed. but the ones with a transform are not.
my code for the whole thing is quite long. so, i'm pasting the relevant parts of the code below. need help badly !
thanks in advance
kota.
import java.io.*;
import java.awt.*;
import java.util.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.event.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.util.Hashtable;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.*;
/*<applet code="iView.class" width=700 height=700>
<param name=tablefile value=1GZM.cts>
<param name=urcfile value=1GZM.urc>
<param name=coorfile value=1GZM.coor>
<param name=transform value="1,0,0,0,1,0,0,0,1">
</applet>*/
publicclass iViewextends Applet{
publicstaticboolean SHALL_I_LISTEN =false;
public JTextArea iViewConsole =new JTextArea(5,5);
public JTextField controlText;
final Color3f colX =new Color3f( 1.0f, 0.0f, 0.0f );
final Color3f colY =new Color3f( 0.0f, 1.0f, 0.0f );
final Color3f colZ =new Color3f( 0.0f, 0.0f, 1.0f );
public String nextLine="";
publicstatic Hashtable<String,String> residueHash13 =new Hashtable<String,String>(7,0.5f);
publicstatic Hashtable<String,String> residueHash31 =new Hashtable<String,String>(7,0.5f);
publicstatic Hashtable<String,String> tableHash=new Hashtable<String,String>(149,0.75f);
publicstatic Hashtable<String,String> urcHash=new Hashtable<String,String>(149,0.75f);
publicstatic Hashtable<String,String> coorHash=new Hashtable<String,String>(149,0.75f);
public TransformGroup selectedResidueTG =new TransformGroup();
public BranchGroup residueGroupBG =new BranchGroup();
public BranchGroup stage =null;
SimpleUniverse simpleU =null;
public iView(){
}
publicvoid init(){
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D =new Canvas3D(config);
add("Center",canvas3D);
JPanel guiPanel =new JPanel();
setupGUI(guiPanel);
add("South",guiPanel);
stage =new BranchGroup();
TransformGroup bgAxes =new TransformGroup();
drawBG(bgAxes);
Text2D labelX =new Text2D("X",new Color3f( 1.0f, 0.0f, 0.0f ),"monospaced", 20, Font.BOLD);
Text2D labelY =new Text2D("Y",new Color3f( 0.0f, 1.0f, 0.0f ),"monospaced", 20, Font.BOLD);
Text2D labelZ =new Text2D("Z",new Color3f( 0.0f, 0.0f, 1.0f ),"monospaced", 20, Font.BOLD);
Transform3D axisXlabel =new Transform3D();
axisXlabel.set(new Vector3f( 0.55f, 0.0f, 0.0f ) );
Transform3D axisYlabel =new Transform3D();
axisYlabel.set(new Vector3f( 0.0f, 0.55f, 0.0f ) );
Transform3D axisZlabel =new Transform3D();
axisZlabel.set(new Vector3f( 0.0f, 0.0f, 0.55f ) );
TransformGroup axisXtg =new TransformGroup(axisXlabel);
axisXtg.addChild(labelX);
TransformGroup axisYtg =new TransformGroup(axisYlabel);
axisYtg.addChild(labelY);
TransformGroup axisZtg =new TransformGroup(axisZlabel);
axisZtg.addChild(labelZ);
residueGroupBG.setCapability(Group.ALLOW_CHILDREN_READ);
residueGroupBG.setCapability(Group.ALLOW_CHILDREN_WRITE);
residueGroupBG.setCapability(Group.ALLOW_CHILDREN_EXTEND);
selectedResidueTG.addChild(residueGroupBG);
selectedResidueTG.addChild(axisXtg);
selectedResidueTG.addChild(axisYtg);
selectedResidueTG.addChild(axisZtg);
selectedResidueTG.addChild(bgAxes);
selectedResidueTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
selectedResidueTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
MouseRotate mrot =new MouseRotate();
mrot.setTransformGroup(selectedResidueTG);
mrot.setSchedulingBounds(new BoundingSphere());
MouseTranslate mtrans =new MouseTranslate();
mtrans.setTransformGroup(selectedResidueTG);
mtrans.setSchedulingBounds(new BoundingSphere());
stage.addChild(mrot);
stage.addChild(mtrans);
stage.addChild(selectedResidueTG);
stage.compile();
simpleU =new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(stage);
}
publicvoid destroy(){
simpleU.cleanup();
}
//removed some code from here
publicvoid showNeighbours(String resin){
String neighbours = urcHash.get(resin);
System.out.println(neighbours);
float[] residueXYZ =newfloat[3];
String[] resXYZ = coorHash.get(resin+":CA").split(":");
for(int i=0; i<resXYZ.length; i++ ) residueXYZ[0] = Float.parseFloat(resXYZ[i]);
String[] neighbourArray = neighbours.split(",");
Sphere[] residueSpheres =new Sphere[neighbourArray.length];
Sphere selectedResidue =new Sphere(0.025f);
TransformGroup[] neighbourTG =new TransformGroup[neighbourArray.length];
String subject;
BranchGroup neighbourBG =new BranchGroup();
Appearance app =new Appearance();
selectedResidue.setAppearance(app);
neighbourBG.setCapability(Group.ALLOW_CHILDREN_WRITE);
neighbourBG.setCapability(Group.ALLOW_CHILDREN_EXTEND);
neighbourBG.removeAllChildren();
for (int i=0; i><neighbourArray.length; i++ ){
if ( residueHash31.get(neighbourArray[i].substring(0,3)) !=null ){
subject = coorHash.get(neighbourArray[i]+":CA");
}else{
subject = coorHash.get(neighbourArray[i]+":HA");
}
float[] neighbourXYZ =newfloat[3];
String[] nghbrXYZ = subject.split(":");
for (int j=0; j><nghbrXYZ.length; j++) neighbourXYZ[j] = Float.parseFloat(nghbrXYZ[j]);
double sphX = ( neighbourXYZ[0] - residueXYZ[0] ) * 0.1;
double sphY = ( neighbourXYZ[1] - residueXYZ[1] ) * 0.1;
double sphZ = ( neighbourXYZ[2] - residueXYZ[2] ) * 0.1;
residueSpheres[i] =new Sphere(0.025f);
residueSpheres[i].setAppearance(app);
Transform3D transXYZ =new Transform3D();
transXYZ.set(new Vector3d(sphX, sphY, sphZ));
neighbourTG[i] =new TransformGroup(transXYZ);
neighbourTG[i].addChild(residueSpheres[i]);
}
for (int i=0; i><neighbourArray.length; i++){
neighbourBG.addChild(neighbourTG[i]);
}
//Text2D t = new Text2D("WHY?", new Color3f(1.0f, 1.0f, 1.0f), "monospaced", 20, Font.BOLD);
//neighbourBG.addChild(t);
Transform3D sr =new Transform3D();
sr.set(new Vector3f(1.0f, 1.0f, 0.0f));
TransformGroup tg =new TransformGroup(sr);
tg.addChild(selectedResidue);
neighbourBG.addChild(tg);
residueGroupBG.addChild(neighbourBG);
}
//more code here
>

