2013-03-12 48 views
3

我剛開始一個圖形任務,這是我第一次使用Java3D。我已經開始試圖建立一把椅子(不要嘲笑以下內容:P)。TransformGroup的正確用法

我只是想知道是否有人可以告訴我正確的使用模式,因爲我很確定我有點偏離。

這裏是我畫:

chair attempt number one

正如你可以看到它是座和箱4個缸的腿。

這裏是我用來繪製它的代碼:

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(); 

    } 

} 

我只是想知道這是否是集團的正確用法,因爲它似乎有很多重複的......如果有人能在這方面略見一斑,我會很感激!

回答

1

這是學習java3d的好機會。採取自制甚至每個腳本,並能夠閱讀,理解和記憶代碼,以便您可以成爲更快更好的程序員。但是,要更好地定義,則需要體驗並強加給你。學習是好的,但爲了真正掌握這一點,你需要遵循自己的方法。因爲每個人在某種程度上都有所不同。請按照您的步驟進行操作,但不要直接踩入縮進。