Forum Stats

  • 3,875,866 Users
  • 2,266,977 Discussions
  • 7,912,359 Comments

Discussions

how to draw a curve dynamically?

843799
843799 Member Posts: 49,999
edited Dec 28, 2009 7:06AM in Java 3D
here is the code to draw a curve:
Shape3D createcurve(){

    int steps = 720;
    float x = 0.1f;
    Point3f[] coords = new Point3f[steps];
    for (int i = 0; i < coords.length; i++) {
        coords[i] = new Point3f(x,(float)Math.pow(2,x),0.0f);
        x += 0.1f;  
    }
    int[] strip = {steps};
    LineStripArray lsa = new LineStripArray(steps, LineStripArray.COORDINATES, strip);
    lsa.setCoordinates(0, coords);
    Appearance app = new Appearance();
    app.setLineAttributes(new LineAttributes(1.5f, LineAttributes.PATTERN_SOLID, true));

        return new Shape3D(lsa, app);
    }
this curve would be added to the BranchGroup, and show.
but I want to draw it dynamically(not show all the curve in one time)
I don't know how to use the Behavior to do this
PLS help me to do it.
THX

Comments

  • 843799
    843799 Member Posts: 49,999
    This might be a old problem, but I was working with Java3D now and I also wanted to do something like this. I finally figured out a solution thought not a good one, but still working one.
    I thought to share if somebody wants to use it in future.
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.GraphicsConfiguration;
    import com.sun.j3d.utils.applet.MainFrame;
    import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
    import com.sun.j3d.utils.universe.*;
    import java.awt.Color;
    import java.util.Enumeration;
    import javax.media.j3d.*;
    import javax.vecmath.*;
    
    
    public class MapTexture extends Applet
    {
        int counter = 720;
        LineStripArray lsa;
        createCurve cur = new createCurve();
        updateCurve updater = new updateCurve();
        BranchGroup createScene()
        {
          BranchGroup objRoot = new BranchGroup();
    
    
          TransformGroup objTransform = new TransformGroup();
          objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
          objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    
          TransformGroup curveTransform = new TransformGroup();
          curveTransform.addChild(cur);
          Behavior behave = new UpdateBehavior();
          behave.setSchedulingBounds(new BoundingSphere());
          curveTransform.addChild(behave);
          objTransform.addChild(curveTransform);
          objRoot.addChild(objTransform);
          return objRoot;
      }
        class updateCurve implements GeometryUpdater
        {
           public void updateData(Geometry geom)
           {
                cur.steps=(cur.steps+1)%counter;
                cur.createGerometry();
            }
        }
        public class UpdateBehavior extends Behavior
        {
              WakeupOnElapsedFrames w = null;
              public UpdateBehavior()
              {
                w = new WakeupOnElapsedFrames(10);
              }
              public void initialize()
              {
                wakeupOn(w);
              }
              public void processStimulus(Enumeration critiria)
              {
                lsa.updateData(updater);
                wakeupOn(w);
              }
        }
    
        public class createCurve extends Shape3D
        {
            int steps;
            Point3d[] coords;
            Color3f[] colors;
            createCurve()
            {
            steps = 0;
            coords = new Point3d[counter];
            colors = new Color3f[counter];
            for(int i=0;i<counter;i++)
                colors[i] = new Color3f(Color.orange);
            int[] strip = {counter};
            lsa = new LineStripArray(counter, LineStripArray.COORDINATES|LineStripArray.COLOR_3|LineStripArray.BY_REFERENCE, strip);
            lsa.setCapability(GeometryArray.ALLOW_REF_DATA_READ);
            lsa.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
    
    
            Appearance app = new Appearance();
            app.setLineAttributes(new LineAttributes(1.5f, LineAttributes.PATTERN_SOLID, true));
            this.setGeometry(lsa);
             }
            public void createGerometry()
            {
                
                float x = 0.1f;
    
                for (int i = 0; i < coords.length; i++) {
                    coords[i] = new Point3d(x,(float)Math.pow(2,x),0.0f);
                    x += 0.1f;
                }
                for(int i=(steps+1);i<counter;i++)
                {
                    coords[i] = coords[steps];
                }
    
                //System.out.println(steps+" "+coords[steps].toString());
                lsa.setCoordRef3d(coords);
                lsa.setColorRef3f(colors);
                
    
            }
    
        }
      public MapTexture ()
      {
        setLayout(new BorderLayout());
        GraphicsConfiguration config =SimpleUniverse.getPreferredConfiguration();
    
        Canvas3D canvas3D = new Canvas3D(config);
        add("Center", canvas3D);
    
        canvas3D.setStereoEnable(false);
    
        SimpleUniverse u = new SimpleUniverse(canvas3D);
        OrbitBehavior orbit = new OrbitBehavior(canvas3D, OrbitBehavior.REVERSE_ALL);
        orbit.setSchedulingBounds(new BoundingSphere());
        ViewingPlatform vp = u.getViewingPlatform();
        vp.setViewPlatformBehavior(orbit);
        u.getViewingPlatform().setNominalViewingTransform();
        u.addBranchGraph(createScene());
      }
    
      public static void main(String argv[])
      {
            new MainFrame(new MapTexture(), 800,800);
      }
    }
  • 791266
    791266 Member Posts: 18,005
    gunjannigam wrote:
    This might be a old problem, but I was working with Java3D now and I also wanted to do something like this. I finally figured out a solution thought not a good one, but still working one.
    I thought to share if somebody wants to use it in future.
    Hi,

    It's nice to see that you want to help, but please, don't resurrect old threads. I'm now locking this one.

    Kaj
This discussion has been closed.