hey there,
I have this java 3D home assignment I need help with. I would greatly appreciate it if someone could direct me to a solution.
I need to build create a 3 D digital clock and add some key navigation to it. I managed to add the key navigation (I removed the code for key navigation to make for less code), but I can't seem to make the necessary animation going... the scene should change every second, or else it will be a frozen clock.
what I did is this:
I built two classes. One to build the clock, and another, behavior class, that I hoped would 'repaint' the scene. I know the classes are interacting, but I am getting a:
javax.media.j3d.RestrictedAccessException: Group: only BranchGroup nodes may be set
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.
here is my code:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.*;
import java.util.*;
public class Detyra3D extends Applet{
Shape3D shape;
public void init(){
setLayout(new BorderLayout());
Canvas3D canv = new Canvas3D(null);
add(canv, BorderLayout.CENTER);
BranchGroup bg = createSceneGraph();
SimpleUniverse su = new SimpleUniverse(canv);
su.getViewingPlatform().setNominalViewingTransform();
su.addBranchGraph(bg);
}
public BranchGroup createSceneGraph(){
BranchGroup scene = new BranchGroup();
Transform3D tr = new Transform3D();
tr.setScale(0.45);
tr.setTranslation(new Vector3f(-0.9f, -0.2f, 0f));
TransformGroup tg = new TransformGroup(tr);
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//this line should make possible my way of overriding
//the processStimulus method in my Behavior class
//to make possible re-reading of tim every half a second
//and building a 3D shape out of it
tg.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE);
scene.addChild(tg);
tg.addChild(shapeClock());//-------------------------------
//set light
PointLight light = new PointLight(new Color3f(Color.red),
new Point3f(1f,1f,1f),
new Point3f(1f,0.1f,0f));
BoundingSphere bounds = new BoundingSphere();
light.setInfluencingBounds(bounds);
scene.addChild(light);
Ndihmese ndihm = new Ndihmese(this, tg);
ndihm.setSchedulingBounds(bounds);
scene.addChild(ndihm);
return scene;
}
//I build a new shape every half a second here
public Shape3D shapeClock(){
Shape3D shape = new Shape3D();
//setting appearance
Appearance ap = new Appearance();
ap.setMaterial(new Material());
//I read time here
Calendar time = new GregorianCalendar();
int hour = time.get(time.HOUR);
int mins = time.get(time.MINUTE);
int secs = time.get(time.SECOND);
//the time shall be a string, so I'm setting the font
Font3D font = new Font3D(new Font("SansSerif", Font.PLAIN, 1),
new FontExtrusion());
//I build the string that represents the current time here
String s = hour + ":" + mins + ":" + secs;
//setting the 3D text that shall be output
Text3D text = new Text3D(font, s);
//creating a 3D shape out of the text above
shape = new Shape3D(text, ap);
//I System.out here to see if my behavior object
//is actually working
System.out.println(s);
return shape;
}
public static void main(String[] args) {
new MainFrame(new Detyra3D(), 720, 480);
}
}
//the following is my behavior class
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.*;
import java.util.*;
import java.util.*;
import javax.media.j3d.*;
public class Ndihmese extends Behavior {
TransformGroup det;
Detyra3D detyra;
public Ndihmese(Detyra3D d3, TransformGroup trg) {
det = trg;
detyra = d3;
}
public void initialize() {
wakeupOn(new WakeupOnElapsedTime(500));
}
public void processStimulus(java.util.Enumeration enumeration) {
det.setChild(detyra.shapeClock(), 0);
//detyra.createSceneGraph();
/*
//this code rotates the scene around z-axis
//I tried it and it worked fine, so I know my
//classes are interacting
int sec = Calendar.getInstance().get(Calendar.SECOND);
Transform3D tr = new Transform3D();
tr.rotZ(-Math.PI * sec /30.0);
det.setTransform(tr);
*/
wakeupOn(new WakeupOnElapsedTime(500));
}
}
Thank you!