Andy,

I guess I want to change the scene on the fly.  I have included a quick
demo of what I am
trying to do.  It is a demo that displays three strings of text.  The user
can change and update
the text by clicking a button.
thanks again for any help or suggestions.
Steve


At 11:44 AM 12/10/99 -0600, you wrote:
>Steve,
>
>Can you be more specific with the problem?
>Is it that you do not know how to physically change shapes on the scene
>graph, or are you having problems changing the scene on the fly (as a
>function of time)?
>
>Andy
>

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.behaviors.mouse.*;
import com.sun.j3d.utils.behaviors.keyboard.*;


class TextProblemJPanel extends JPanel
{
                String []my_list = new String[3];
          Button updateB    = new Button("Update");

    JTextField field1 = new JTextField(10);
    JTextField field2 = new JTextField(10);
    JTextField field3 = new JTextField(10);


    public TextProblemJPanel(String sOne, String sTwo, String sThree)
    {
                        my_list[0] = sOne;
                        my_list[1] = sTwo;
                        my_list[2] = sThree;

                                        this.setPreferredSize(new Dimension(400,400));

                                        setLayout(new BorderLayout());
                Canvas3D canvas3D = new Canvas3D(null);
                add("Center", canvas3D);

                Vector3f translate = new Vector3f();
                Transform3D t3d = new Transform3D();
                TransformGroup tg = null;

                SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
                tg = simpleU.getViewingPlatform().getViewPlatformTransform();

                translate.set(0.0f,0.0f, 1.0f);
                t3d.setTranslation(translate);

                tg.setTransform(t3d);

                simpleU.addBranchGraph(createSceneGraph());


        JPanel instructionPanel = new JPanel(new GridLayout(0,1));

        instructionPanel.add(field1);
        field1.setText(my_list[0]);
        instructionPanel.add(field2);
        field2.setText(my_list[1]);
        instructionPanel.add(field3);
        field3.setText(my_list[2]);
              instructionPanel.add(updateB);

                                add(instructionPanel, BorderLayout.SOUTH);


                    // create a mouse listener
                MouseListen mListen = new MouseListen();

                // register listeners
                                updateB.addMouseListener(mListen);

    }

    // setup font stuff
    private String fontName = "TestFont";
    // create 3D text
    Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 1), new FontExtrusion());

  public void createMethodRepresentation( double x, double y, double z, String 
methName, TransformGroup objScale, BoundingSphere bounds, boolean allow_billboard )
  {
     float slen = methName.length();
     float adjust_x_len = (float)(x - (slen/3.0f));

     Transform3D sphereMat = new Transform3D();
     TransformGroup sphereTrans = new TransformGroup(sphereMat);

     sphereTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
     sphereTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

                ///////////////////////////////////////////////

    Text3D txt = new Text3D(f3d, methName, new Point3f( (float)adjust_x_len, 
(float)(y), (float)z));
    Shape3D textShape = new Shape3D();
    textShape.setGeometry(txt);

    Appearance apText = new Appearance();
    Material m = new Material();
    m.setLightingEnable(true);
    // m.setAmbientColor(0.0f,0.0f,0.0f); // r,g,b
    m.setAmbientColor( new Color3f(0.0f,1.0f,0.0f) );  // have to pass in color
        m.setDiffuseColor( new Color3f(0.0f,1.0f,0.0f) );  // have to pass in color

    apText.setMaterial(m);
    textShape.setAppearance(apText);

        ///////////////////////////////////////

   // bboardY.setSchedulingBounds( bounds );
   sphereTrans.addChild( textShape );

   objScale.addChild( sphereTrans );

 }

    public BranchGroup createSceneGraph()
    {

                          // Create the root of the branch graph
                          BranchGroup objRoot = new BranchGroup();
                          TransformGroup objScale = new TransformGroup();
                          Transform3D textMat = new Transform3D();
                          textMat.setScale(0.05);
                          objScale.setTransform(textMat);

                         TransformGroup objTrans = new TransformGroup();
                         objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
                         objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
       objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
                         objRoot.addChild(objTrans);

                         objTrans.addChild(objScale);


                          BoundingSphere bounds = new BoundingSphere(new 
Point3d(0.0,0.0,0.0), 1000.0);

                                for ( int i = 0; i < 3; i++ )
                {
                        createMethodRepresentation( 0.0,  -i,  0.0,  my_list[i], 
objScale, bounds, true );
                }

                                setupRotations( bounds, objRoot, objTrans );
                                setupLights( bounds, objRoot );

                                return objRoot;

    }

  public void setupRotations( BoundingSphere bounds, BranchGroup objRoot, 
TransformGroup objTrans )
  {

              // Create the rotate behavior node
              MouseRotate behavior = new MouseRotate();
              behavior.setTransformGroup(objTrans);
              objTrans.addChild(behavior);
              behavior.setSchedulingBounds(bounds);

              // Create the zoom behavior node
              MouseZoom behavior2 = new MouseZoom();
              behavior2.setTransformGroup(objTrans);
              objTrans.addChild(behavior2);
              behavior2.setSchedulingBounds(bounds);

              // Create the translate behavior node
              MouseTranslate behavior3 = new MouseTranslate();
              behavior3.setTransformGroup(objTrans);
              objTrans.addChild(behavior3);
              behavior3.setSchedulingBounds(bounds);

    }

  public void setupLights( BoundingSphere bounds, BranchGroup objRoot )
  {
        // Set up the ambient light
        Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
        ambientLightNode.setInfluencingBounds(bounds);
        objRoot.addChild(ambientLightNode);

        // Set up the directional lights
        Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
        Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);
        Color3f light2Color = new Color3f(0.3f, 0.3f, 0.4f);
        Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);

        DirectionalLight light1
            = new DirectionalLight(light1Color, light1Direction);
        light1.setInfluencingBounds(bounds);
        objRoot.addChild(light1);

        DirectionalLight light2
            = new DirectionalLight(light2Color, light2Direction);
        light2.setInfluencingBounds(bounds);
        objRoot.addChild(light2);
  }

    protected class MouseListen extends MouseAdapter
    {
        public void mouseReleased(MouseEvent evt)
        {
            Object obj = evt.getSource();

            if(obj == updateB )
            {
                 String msg = "When you click on the button I want the text to update 
the view.";
                                             
JOptionPane.showMessageDialog(null,msg,"Error",JOptionPane.ERROR_MESSAGE);

                                 my_list[0] = field1.getText();
                                 my_list[1] = field2.getText();
                                 my_list[2] = field3.getText();

                                 msg = my_list[0] + "\n" + my_list[1] + "\n" + 
my_list[2] + "\n";
                                             
JOptionPane.showMessageDialog(null,msg,"Error",JOptionPane.ERROR_MESSAGE);
            }
                }
    }


    public static void main (String args[])
    {
        JFrame frame = new JFrame("Text View");

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        frame.setContentPane(new TextProblemJPanel("string 1","string 2","string 3"));
        frame.pack();
        frame.setVisible(true);
    }


}

Reply via email to