3

我刚刚开始了一项图形作业,这是我第一次使用 Java3D。我开始尝试建造一把椅子(不要嘲笑以下内容:P)。

我只是想知道是否有人可以告诉我正确的使用模式,因为我很确定我有点不对劲。

这是我画的:

椅子尝试第一

如您所见,它是一个用于座椅的盒子和 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();

    }

}

我只是想知道这是否是组的正确用法,因为似乎有很多重复...如果有人能对此有所了解,我将不胜感激!

4

1 回答 1

1

这是学习 java3d 的良好一步。使用自制甚至按制作的脚本,并能够阅读、理解和记忆代码,这样你就可以成为一个更快、更好的程序员。但是要更好地定义是把经验强加给你。学习是好的,但为了真正掌握这一点,你需要遵循自己的方法。因为每个人在某些方面都是不同的。跟随你的脚步,但不要直接踏入缩进。

于 2013-03-14T01:10:03.897 回答