java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow Java3D arrow How to use GeometryArray for BY_REFERENCE
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4089
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use GeometryArray for BY_REFERENCE E-mail
User Rating: / 7
PoorBest 

When a BY_REFERENCE GeometryArray object is created, the user creates geometry arrays and populates those arrays with geometry data. There are no geometry arrays internal to the Geometry object no copy of the geometry data made. Instead the BY_REFERENCE GeometryArray object simply stores a reference to the user array(s) of geometry data.

There are two good reasons for using BY_REFERENCE GeometryArray:

  1. if the geometry data is dynamic
  2. if the geometry data requires too much memory

This program shows how to implement the dynamic geometry data by a BY_REFERENCE GeometryArray.


Image

public class DynamicCylinder extends JFrame {
  private float height = 0.5f;
  private float radius = 0.02f;
  private int verticesNum = 40;
  private float[] vertices = new float[verticesNum * 3];
  private Shape3D cylinder=new Shape3D(createGeometry());
  
  private JTextField heightTxt=new JTextField("0.5",5);
  private JTextField radiusTxt=new JTextField("0.02",5);
  
  public DynamicCylinder() {
    ...
    
    // Prepare the control panel for changing the height and radius of 
    // the cylinder
    JPanel panel=new JPanel();
    panel.add(new JLabel("Height:"));
    panel.add(heightTxt);
    panel.add(new JLabel("Radius:"));
    panel.add(radiusTxt);
    JButton okBtn=new JButton("Ok");
    okBtn.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e) {
        setHeight(Float.parseFloat(heightTxt.getText()));
        setRadius(Float.parseFloat(radiusTxt.getText()));
      }
      
    });
    panel.add(okBtn);
    getContentPane().add(panel,BorderLayout.SOUTH);
  }

  public BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    // Set the proper capabilities
    cylinder.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    
    objRoot.addChild(cylinder);

    return objRoot;
  }

  private Geometry createGeometry() {
    updateVertices();

    // Create the geometry by reference
    TriangleStripArray quadArray = new TriangleStripArray(verticesNum,
        GeometryArray.COORDINATES | GeometryArray.BY_REFERENCE,
        new int[] { verticesNum });
    quadArray.setCoordRefFloat(vertices);
    // Set the capabilities
    quadArray.setCapability(GeometryArray.ALLOW_REF_DATA_READ);
    quadArray.setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);

    return quadArray;
  }
  
  private void updateVertices() {
      float perR = (float) (* Math.PI / (verticesNum / 1));      
      for (int i = 0; i < verticesNum; i += 2) {
        vertices[ (i + 13= vertices[i *
            3(float) (radius * Math.cos(i / * perR));
        vertices[ (i + 12= vertices[i * +
            2(float) (radius * Math.sin(i / * perR));
        vertices[i * 1= height;
        vertices[ (i + 110;
      }
    }
  
  public void setHeight(float height) {
      float oldHeight = this.height;
      this.height = height;

      // Update the geometry
      this.updateGeometry();
    }
  
  public void setRadius(float radius) {
      float oldRadius = this.radius;
      this.radius = radius;

      // Update the geometry
      this.updateGeometry();
    }
  
  private void updateGeometry() {
      GeometryArray geometryArray = (GeometryArray)cylinder.getGeometry();
      geometryArray.updateData(new GeometryUpdater(){
        public void updateData(Geometry geometry){
          updateVertices();
        }
      });
    }

  public static void main(String[] args) {
    ...
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.