error when I try to reset the child (a 3d shape representing time) of the TransformGroup in my code. However, I get no error when I invoke the createSceneGraph() method through my behavior class, but the clock remains frozen.

Sponsored Links

here is my code:

Java Syntax (Toggle Plain Text)

Code:
 1.
      import java.awt.*;
   2.
      import java.applet.*;
   3.
      import java.awt.event.*;
   4.
      import javax.media.j3d.*;
   5.
      import javax.vecmath.*;
   6.
      import com.sun.j3d.utils.universe.*;
   7.
      import com.sun.j3d.utils.geometry.*;
   8.
      import com.sun.j3d.utils.applet.MainFrame;
   9.
      import com.sun.j3d.utils.behaviors.keyboard.*;
  10.
      import java.util.*;
  11.
       
  12.
      public class Detyra3D extends Applet{
  13.
      Shape3D shape;
  14.
       
  15.
       
  16.
      public void init(){
  17.
      setLayout(new BorderLayout());
  18.
      Canvas3D canv = new Canvas3D(null);
  19.
      add(canv, BorderLayout.CENTER);
  20.
       
  21.
      BranchGroup bg = createSceneGraph();
  22.
       
  23.
      SimpleUniverse su = new SimpleUniverse(canv);
  24.
      su.getViewingPlatform().setNominalViewingTransform();
  25.
       
  26.
       
  27.
       
  28.
       
  29.
      su.addBranchGraph(bg);
  30.
       
  31.
       
  32.
       
  33.
       
  34.
      }
  35.
       
  36.
      public BranchGroup createSceneGraph(){
  37.
      BranchGroup scene = new BranchGroup();
  38.
       
  39.
       
  40.
       
  41.
       
  42.
       
  43.
      Transform3D tr = new Transform3D();
  44.
      tr.setScale(0.45);
  45.
      tr.setTranslation(new Vector3f(-0.9f, -0.2f, 0f));
  46.
       
  47.
       
  48.
       
  49.
       
  50.
       
  51.
      TransformGroup tg = new TransformGroup(tr);
  52.
      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  53.
       
  54.
       
  55.
      //this line should make possible my way of overriding
  56.
      //the processStimulus method in my Behavior class
  57.
      //to make possible re-reading of tim every half a second
  58.
      //and building a 3D shape out of it
  59.
      tg.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
  60.
      scene.addChild(tg);
  61.
       
  62.
       
  63.
       
  64.
      tg.addChild(shapeClock());//-------------------------------
  65.
       
  66.
       
  67.
      //set light
  68.
      PointLight light = new PointLight(new Color3f(Color.red),
  69.
      new Point3f(1f,1f,1f),
  70.
      new Point3f(1f,0.1f,0f));
  71.
      BoundingSphere bounds = new BoundingSphere();
  72.
      light.setInfluencingBounds(bounds);
  73.
      scene.addChild(light);
  74.
       
  75.
      Ndihmese ndihm = new Ndihmese(this, tg);
  76.
      ndihm.setSchedulingBounds(bounds);
  77.
      scene.addChild(ndihm);
  78.
       
  79.
      return scene;
  80.
       
  81.
      }
  82.
       
  83.
      //I build a new shape every half a second here
  84.
      public Shape3D shapeClock(){
  85.
       
  86.
       
  87.
       
  88.
      Shape3D shape = new Shape3D();
  89.
      //setting appearance
  90.
      Appearance ap = new Appearance();
  91.
      ap.setMaterial(new Material());
  92.
       
  93.
       
  94.
      //I read time here
  95.
      Calendar time = new GregorianCalendar();
  96.
      int hour = time.get(time.HOUR);
  97.
      int mins = time.get(time.MINUTE);
  98.
      int secs = time.get(time.SECOND);
  99.
       
 100.
       
 101.
      //the time shall be a string, so I'm setting the font
 102.
      Font3D font = new Font3D(new Font("SansSerif", Font.PLAIN, 1),
 103.
      new FontExtrusion());
 104.
       
 105.
       
 106.
      //I build the string that represents the current time here
 107.
      String s = hour + ":" + mins + ":" + secs;
 108.
       
 109.
       
 110.
       
 111.
      //setting the 3D text that shall be output
 112.
      Text3D text = new Text3D(font, s);
 113.
      //creating a 3D shape out of the text above
 114.
      shape = new Shape3D(text, ap);
 115.
      //I System.out here to see if my behavior object
 116.
      //is actually working
 117.
      System.out.println(s);
 118.
      return shape;
 119.
      }
 120.
       
 121.
       
 122.
       
 123.
       
 124.
      public static void main(String[] args) {
 125.
      new MainFrame(new Detyra3D(), 720, 480);
 126.
      }
 127.
       
 128.
      }
 129.
       
 130.
      //the following is my behavior class
 131.
       
 132.
      import java.awt.*;
 133.
      import java.applet.*;
 134.
      import java.awt.event.*;
 135.
      import javax.media.j3d.*;
 136.
      import javax.vecmath.*;
 137.
      import com.sun.j3d.utils.universe.*;
 138.
      import com.sun.j3d.utils.geometry.*;
 139.
      import com.sun.j3d.utils.applet.MainFrame;
 140.
      import com.sun.j3d.utils.behaviors.keyboard.*;
 141.
      import java.util.*;
 142.
      import java.util.*;
 143.
      import javax.media.j3d.*;
 144.
       
 145.
      public class Ndihmese extends Behavior {
 146.
      TransformGroup det;
 147.
      Detyra3D detyra;
 148.
      public Ndihmese(Detyra3D d3, TransformGroup trg) {
 149.
      det = trg;
 150.
      detyra = d3;
 151.
      }
 152.
       
 153.
      public void initialize() {
 154.
      wakeupOn(new WakeupOnElapsedTime(500));
 155.
      }
 156.
       
 157.
      public void processStimulus(java.util.Enumeration enumeration) {
 158.
      det.setChild(detyra.shapeClock(), 0);
 159.
      //detyra.createSceneGraph();
 160.
      /*
 161.
        //this code rotates the scene around z-axis
 162.
        //I tried it and it worked fine, so I know my
 163.
        //classes are interacting
 164.
        int sec = Calendar.getInstance().get(Calendar.SECOND);
 165.
        Transform3D tr = new Transform3D();
 166.
        tr.rotZ(-Math.PI * sec /30.0);
 167.
        det.setTransform(tr);
 168.
        */
 169.
      wakeupOn(new WakeupOnElapsedTime(500));
 170.
      }
 171.
      }