Dear all,

I am trying to make an animation of my scene. I have thousands of
Shape3D objects.

I want to wake up different Shape3Ds at different times. e.g. every
three Shape3Ds with the interval of two seconds?

I attached a sample program that tries to do this. As you see there are
four cubes on the scene. I want to wake up the first two, 2 seconds
after I press the button and the last two 5 seconds after I press the
button (3 seconds after the first two appears on the scene). I know that
the use of switch is not right in the example but I am working about the
Time Behaviour class.

Can any one help me about the attached code?

<<inline: Image1.jpg>>

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.media.j3d.WakeupOnElapsedTime;


public class TestSwitch extends Applet
{
        boolean willCrash = true;
        Switch switchG;
        static BranchGroup root;
        static TransformGroup TG1;
        static TransformGroup TG2;
        static TransformGroup TG3;
        static TransformGroup TG4;
        static Switch SW1;
        static Switch SW2;
        static Switch SW3;
        static Switch SW4;
        static Behavior bh;
//      private WakeupOnElapsedTime critter;

        public BranchGroup createScene()
        {
                root = new BranchGroup();
                root.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);


                TG1 = new TransformGroup();
                TG2 = new TransformGroup();
                TG3 = new TransformGroup();
                TG4 = new TransformGroup();

                Transform3D T1 = new Transform3D();
                Transform3D T2 = new Transform3D();
                Transform3D T3 = new Transform3D();
                Transform3D T4 = new Transform3D();

                T1.set( new Vector3d( -0.45, 0, 0 ) );
                T2.set( new Vector3d( -0.15, 0, 0 ) );
                T3.set( new Vector3d( +0.15, 0, 0 ) );
                T4.set( new Vector3d( +0.45, 0, 0 ) );

                TG1.setTransform( T1 );
                TG2.setTransform( T2 );
                TG3.setTransform( T3 );
                TG4.setTransform( T4 );

                SW1 = new Switch();
                SW2 = new Switch();
                SW3 = new Switch();
                SW4 = new Switch();

                SW1.setWhichChild(Switch.CHILD_NONE);
                SW2.setWhichChild(Switch.CHILD_NONE);
                SW3.setWhichChild(Switch.CHILD_NONE);
                SW4.setWhichChild(Switch.CHILD_NONE);

                SW1.setCapability(Switch.ALLOW_SWITCH_READ);
                SW1.setCapability(Switch.ALLOW_SWITCH_WRITE);
                SW2.setCapability(Switch.ALLOW_SWITCH_READ);
                SW2.setCapability(Switch.ALLOW_SWITCH_WRITE);
                SW3.setCapability(Switch.ALLOW_SWITCH_READ);
                SW3.setCapability(Switch.ALLOW_SWITCH_WRITE);
                SW4.setCapability(Switch.ALLOW_SWITCH_READ);
                SW4.setCapability(Switch.ALLOW_SWITCH_WRITE);

                TG1.addChild(SW1);
                TG2.addChild(SW2);
                TG3.addChild(SW3);
                TG4.addChild(SW4);

                SW1.addChild(new ColorCube(0.1));
                SW2.addChild(new ColorCube(0.1));
                SW3.addChild(new ColorCube(0.1));
                SW4.addChild(new ColorCube(0.1));


                System.out.println("TimeBehaviour added");


                root.addChild(TG1);
                root.addChild(TG2);
                root.addChild(TG3);
                root.addChild(TG4);

                return root;
        }
        public TestSwitch()
        {
                GraphicsConfiguration config = 
SimpleUniverse.getPreferredConfiguration();
                Canvas3D canvas = new Canvas3D(config);
                SimpleUniverse universe = new SimpleUniverse(canvas);
                universe.getViewingPlatform().setNominalViewingTransform();
                universe.addBranchGraph( createScene() );

                setLayout(new BorderLayout());
                add("Center", canvas);
                Button switcher = new Button( "Switch" );
                switcher.addActionListener( new ActionListener()
                {
                        public void actionPerformed( ActionEvent e )
                        {
                                System.out.println("Switch1");

                                BranchGroup bg = new BranchGroup();

                                bh = new TimeBehaviour(SW1, SW2, SW3, SW4);
                                System.out.println("TimeBehaviour created");
                                BoundingSphere bounds_bh = new BoundingSphere();
                                bh.setSchedulingBounds(bounds_bh);

                                bg.addChild(bh);
                                root.addChild(bg);

                        }
                });
                add( "South", switcher );
        }

        public static void main( String[] arg )
        {
                new MainFrame(new TestSwitch(), 256, 256);
        }

}
import java.awt.AWTEvent;
import javax.media.j3d.Behavior;
import java.util.Enumeration;
import javax.media.j3d.WakeupOnElapsedTime;
import javax.media.j3d.WakeupCondition;
import java.awt.event.MouseEvent;
import javax.media.j3d.*;


// BASE CLASS FOR MOUSE BEHAVIOURS
public class TimeBehaviour extends Behavior
{
        private long eventMask;
        private WakeupOnElapsedTime critter;

        static Switch Switch1;
        static Switch Switch2;
        static Switch Switch3;
        static Switch Switch4;


        public TimeBehaviour(Switch SW1, Switch SW2, Switch SW3, Switch SW4)
        {
                Switch1 = SW1;
                Switch2 = SW2;
                Switch3 = SW3;
                Switch4 = SW4;
        }

        public void initialize()
        {
                System.out.println("INITIALIZED");
                critter = new WakeupOnElapsedTime(2000);
                resetEvents();
        }

        protected final void resetEvents()
        {
                wakeupOn(critter);
        }


        public void processStimulus(Enumeration criteria)
        {
                System.out.println("RUNNING PROCESS STIMULUS");
                WakeupCondition cond;

                while(criteria.hasMoreElements())
                {
                        cond = (WakeupCondition)criteria.nextElement();
                        if(cond instanceof WakeupOnElapsedTime)
                        {
                                System.out.println("WAKE UP");

                                Switch1.setWhichChild(Switch.CHILD_ALL);
                                Switch2.setWhichChild(Switch.CHILD_ALL);
                                Switch3.setWhichChild(Switch.CHILD_ALL);
                                Switch4.setWhichChild(Switch.CHILD_ALL);
                        }
                }
        }
}

Reply via email to