I have just started a graphics assignment and this is my first use of Java3D. I have started off by attempting to build a chair (don't laugh at the following :P).
I just wanted to know if somebody could tell me the correct usage pattern because I am pretty sure I am a little bit off.
Here is what I have drawn:

As you can see it is a box for the seat and 4 cylinders for the legs.
Here is the code I used to draw it:
public class Chair {
public Chair() {
SimpleUniverse universe = new SimpleUniverse();
BranchGroup group = new BranchGroup();
// Main seat component
Box box = new Box(.39f,.03f,.37f, new Appearance());
TransformGroup seatGroup = new TransformGroup();
Transform3D transform = new Transform3D();
Vector3f vector = new Vector3f(.0f, .0f, .0f);
transform.setTranslation(vector);
seatGroup.setTransform(transform);
seatGroup.addChild(box);
// Legs of chair
Cylinder leg1 = new Cylinder(.07f, .45f);
Cylinder leg2 = new Cylinder(.07f, .45f);
Cylinder leg3 = new Cylinder(.07f, .45f);
Cylinder leg4 = new Cylinder(.07f, .45f);
//Create new TransformGroup, vector position for each leg
TransformGroup leg1Group = new TransformGroup();
Transform3D legTransform = new Transform3D();
Vector3f leg1Position = new Vector3f(-.37f, -0.225f, -.35f);
legTransform.setTranslation(leg1Position);
leg1Group.setTransform(legTransform);
leg1Group.addChild(leg1);
seatGroup.addChild(leg1Group);
TransformGroup leg2Group = new TransformGroup();
Transform3D leg2Transform = new Transform3D();
Vector3f leg2Position = new Vector3f(.37f, -0.225f, .35f);
leg2Transform.setTranslation(leg2Position);
leg2Group.setTransform(leg2Transform);
leg2Group.addChild(leg2);
seatGroup.addChild(leg2Group);
TransformGroup leg3Group = new TransformGroup();
Transform3D leg3Transform = new Transform3D();
Vector3f leg3Position = new Vector3f(.37f, -0.225f, -.35f);
leg3Transform.setTranslation(leg3Position);
leg3Group.setTransform(leg3Transform);
leg3Group.addChild(leg3);
seatGroup.addChild(leg3Group);
TransformGroup leg4Group = new TransformGroup();
Transform3D leg4Transform = new Transform3D();
Vector3f leg4Position = new Vector3f(-.37f, -0.225f, .35f);
leg4Transform.setTranslation(leg4Position);
leg4Group.setTransform(leg4Transform);
leg4Group.addChild(leg4);
seatGroup.addChild(leg4Group);
//Add seat group to main object group
group.addChild(seatGroup);
//Stuff to see the universe//
Color3f light1Color = new Color3f(.1f, 1.4f, .1f); // green light
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
group.addChild(light1);
universe.getViewingPlatform().setNominalViewingTransform();
// add the group of objects to the Universe
universe.addBranchGraph(group);
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
ViewingPlatform viewPlatform = universe.getViewingPlatform();
BoundingSphere boundingSphere = new BoundingSphere(new Point3d(0f, 0f, 0f), 100f);
// Add a behaviour to viewPlatform, allowing the user to rotate, zoom, and straff the scene
OrbitBehavior orbitBehaviour = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
orbitBehaviour.setSchedulingBounds(boundingSphere);
viewPlatform.setViewPlatformBehavior(orbitBehaviour);
}
public static void main(String[] args) {
new Chair();
}
}
I just want to know if this is the correct usage of the groups as it seems there is a lot of duplication... If anybody could shed some light on this I would appreciate it a lot !!
This is a good step towards learning java3d. Taking homemade or even per made script and being able to read, understand and memorizing the code so you can become a faster and better programmer. But to define better is take ones experience and force it onto you. Learning is good but in order to truly master this you need to follow your own methods. because Every person is different in some way. follow the foot steps of your per's but don't step directly in the indent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With