Box NAMEHERE = new Box() - can't change NAMEHERE?!

Java3d will not let me use a string for this name? i want a series of box objects all with different names. as i have one class and i pass a name to it to create the differet named instances
but i get an error? anyone help please?
error:
C:\My Documents\My Dis\j3D\3D>javac *.java
createBox.java:35: objectName is already defined in createBox(ja
Box objectName = new Box(0.1f, 0.1f, 0.1f, app);
^
code:
public class createBox
     private BranchGroup boxBG;
     public createBox(String objName)
          String objectName = "box";
          System.out.println("createBox: create(): Constructor...");
     boxBG = new BranchGroup();
     //colour of object
     ColoringAttributes ca = new ColoringAttributes();
     ca.setColor(1.0f, 1.0f, 0.0f);
     Appearance app = new Appearance();
     app.setColoringAttributes(ca);
          //move box into view
          Transform3D translate = new Transform3D();
     translate.set(new Vector3f(0.1f, 0.0f, 0.0f)); //its coordinates
     TransformGroup boxTGT1 = new TransformGroup(translate); //create transform group
          //create a box
          System.out.println("createBox: createBox(): Creating Box called: "+objectName);
     Box objectName = new Box(0.1f, 0.1f, 0.1f, app);
     objectName.setAppearance(app);
          //relationships of box
     boxTGT1.addChild(objectName); //'add' translation to the created box
     boxBG.addChild(boxTGT1); //adding the final branch group to the scene
          //optimizing the branch group
          System.out.println("createBox: createBox(): Compiling BranchGroup...");
          boxBG.compile();
          public BranchGroup getBG()
          return boxBG;
}

You have
String objectName = "box";
and
Box objectName = new Box(0.1f, 0.1f, 0.1f, app);
Meaning you're trying to make a String called objectName, then a Box called objectName - not the value of objectName, but the name itself. Hence the error.
It's not that Java3D specifically won't let you use a string for a name, it's that the entire Java language won't, because it doesn't work that way.
You need to 'name' your boxes when you write the program, not at runtime - e.g.
Box myFirstBox = new Box(0.1f, 0.1f, 0.1f, new Appearance());
Box mySecondBox = new Box(0.1f, 0.1f, 0.1f, new Appearance());
Box myThirdBox = new Box(0.1f, 0.1f, 0.1f, new Appearance());Or, if you want, say, 100 boxes:
Box[] myLotsOfBoxes;
for(int n=0; n<100; n++)
    myLotsOfBoxes[n]= new Box(0.1f, 0.1f, 0.1f, new Appearance());
    }You cannot name variables when the program is running for a whole lot of pretty fundamental reasons. If you need to change the 'name' when the program is running, you need to add a getName and setName method to your own extension of Box, that add a name property. This isn't the variable name itself, but it lets you assign names to your boxes as the program runs.
e.g.
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
public class namedBox extends Box
   private String name;
   public namedBox(float x, float y, float z, Appearance a, String boxName)
   super(x,y,z,a);
   setName(boxName);
   public void setName(String newName)
   name = newName;
   public String getName()
   return name;
}

Similar Messages

Maybe you are looking for