Example usage for javax.media.j3d Canvas3D Canvas3D

List of usage examples for javax.media.j3d Canvas3D Canvas3D

Introduction

In this page you can find the example usage for javax.media.j3d Canvas3D Canvas3D.

Prototype

public Canvas3D(GraphicsConfiguration graphicsConfiguration) 

Source Link

Document

Constructs and initializes a new Canvas3D object that Java 3D can render into.

Usage

From source file:PureImmediateStereo.java

public void init() {
    setLayout(new BorderLayout());

    // Preferred to use Stereo
    GraphicsConfigTemplate3D gct = new GraphicsConfigTemplate3D();
    gct.setStereo(GraphicsConfigTemplate3D.PREFERRED);

    GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getBestConfiguration(gct);/*from  w w  w  .ja v a2  s  .co  m*/

    canvas = new Canvas3D(config);
    Map map = canvas.queryProperties();

    stereoSupport = canvas.getStereoAvailable();

    if (stereoSupport) {
        System.out.println(
                "This machine support stereo, you should see a red cone on the left and green cone on the right.");
        // User can overide the above default behavior using
        // java3d property.
        String str = System.getProperty("j3d.sharedstereozbuffer", defaultSharedStereoZbuffer);
        sharedStereoZbuffer = (new Boolean(str)).booleanValue();
    } else {
        System.out.println("Stereo is not support, you should only see the left red cone.");
    }

    if (!canvas.getDoubleBufferAvailable()) {
        System.out.println("Double buffer is not support !");
    }

    // we must stop the Renderer in PureImmediate mode
    canvas.stopRenderer();
    add("Center", canvas);

    // Create the universe and viewing branch
    u = new SimpleUniverse(canvas);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();

    // Start a new thread that will continuously render
    (new Thread(this)).start();
}

From source file:SpotLightApp.java

public SpotLightApp() {
    BoundingSphere bound1 = new BoundingSphere(new Point3d(0.0, 0.9, 0.0), 0.5);
    BoundingSphere bound2 = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 0.5);
    BoundingSphere bound3 = new BoundingSphere(new Point3d(0.0, -0.9, 0.0), 0.5);

    Vector3f direction = new Vector3f(0.0f, 0.0f, -1.0f);

    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);

    BranchGroup scene = new BranchGroup();

    final int X = 101, Y = 102;
    final float dx = 0.01f, dy = -0.01f;
    final float epx = dx / 2.0f, epy = dy / 2.0f;
    final float top = 0.5f, left = -0.5f;
    final float right = left + dx * (X - 1);
    final float bottom = top + dy * (Y - 1);

    IndexedQuadArray qa = new IndexedQuadArray(X * Y, QuadArray.COORDINATES | QuadArray.NORMALS,
            (X - 1) * (Y - 1) * 4);//  ww w.  j  ava 2 s . co  m

    float x, y;
    int i = 0;
    System.out.print("set " + X * Y + " coordiantes.....  ");
    for (y = top; y >= bottom - epx; y += dy)
        for (x = left; x <= right + epx; x += dx)
            qa.setCoordinate(i++, new Point3f(x, y, 0.0f));

    System.out.println(i + " coordiantes done");
    int row, col;
    i = 0;
    Vector3f n = new Vector3f(0.0f, 0.0f, 1.0f);

    System.out.print("set " + (X - 1) * (Y - 1) * 4 + " coordinate indices.....  ");
    for (row = 0; row < (Y - 1); row++) {
        for (col = 0; col < (X - 1); col++) {
            qa.setNormal(row * X + col, n);
            qa.setCoordinateIndex(i++, row * X + col);
            qa.setCoordinateIndex(i++, (row + 1) * X + col);
            qa.setCoordinateIndex(i++, (row + 1) * X + col + 1);
            qa.setCoordinateIndex(i++, row * X + col + 1);
        }
        qa.setNormal(row * X + col + 1, n);
    }
    System.out.println(i + " coordinate indices done");

    for (col = 0; col < (X - 1); col++) {
        qa.setNormal(X * (Y - 1) + 1 + col, n);
    }
    System.out.println("coordinate normals done");

    Appearance qAppear = createMatAppear(blue, white, 5.0f);
    Shape3D plane = new Shape3D(qa, qAppear);

    Transform3D translate = new Transform3D();
    translate.set(new Vector3f(-0.5f, 0.5f, 0.0f));
    TransformGroup tg1 = new TransformGroup(translate);
    scene.addChild(tg1);
    Shape3D plane1 = new Shape3D(qa, qAppear);
    plane1.setBounds(bound1);
    tg1.addChild(plane1);

    translate.set(new Vector3f(0.5f, 0.5f, 0.0f));
    TransformGroup tg2 = new TransformGroup(translate);
    scene.addChild(tg2);
    Shape3D plane2 = new Shape3D(qa, qAppear);
    plane2.setBounds(bound1);
    tg2.addChild(plane2);

    translate.set(new Vector3f(-0.5f, -0.5f, 0.0f));
    TransformGroup tg3 = new TransformGroup(translate);
    scene.addChild(tg3);
    Shape3D plane3 = new Shape3D(qa, qAppear);
    plane3.setBounds(bound3);
    tg3.addChild(plane3);

    translate.set(new Vector3f(0.5f, -0.5f, 0.0f));
    TransformGroup tg4 = new TransformGroup(translate);
    scene.addChild(tg4);
    Shape3D plane4 = new Shape3D(qa, qAppear);
    plane4.setBounds(bound3);
    tg4.addChild(plane4);

    AmbientLight lightA = new AmbientLight();
    lightA.setInfluencingBounds(new BoundingSphere());
    scene.addChild(lightA);

    scene.addChild(newSpotLight(bound1, new Point3f(-0.7f, 0.7f, 0.5f), 0.1f, 5.0f));
    scene.addChild(newSpotLight(bound1, new Point3f(0.0f, 0.7f, 0.5f), 0.1f, 50.0f));
    scene.addChild(newSpotLight(bound1, new Point3f(0.7f, 0.7f, 0.5f), 0.1f, 100.0f));
    scene.addChild(newSpotLight(bound2, new Point3f(-0.7f, 0.0f, 0.5f), 0.3f, 5.0f));
    scene.addChild(newSpotLight(bound2, new Point3f(0.0f, 0.0f, 0.5f), 0.3f, 50.0f));
    scene.addChild(newSpotLight(bound2, new Point3f(0.7f, 0.0f, 0.5f), 0.3f, 100.0f));
    scene.addChild(newSpotLight(bound3, new Point3f(-0.7f, -0.7f, 0.5f), 0.5f, 5.0f));
    scene.addChild(newSpotLight(bound3, new Point3f(0.0f, -0.7f, 0.5f), 0.5f, 50.0f));
    scene.addChild(newSpotLight(bound3, new Point3f(0.7f, -0.7f, 0.5f), 0.5f, 100.0f));

    Background background = new Background();
    background.setApplicationBounds(new BoundingSphere());
    background.setColor(1.0f, 1.0f, 1.0f);
    scene.addChild(background);

    scene.compile();

    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(null);
    add("Center", c);

    SimpleUniverse u = new SimpleUniverse(c);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();

    u.addBranchGraph(scene);
}

From source file:IntersectTest.java

public void init() {
    setLayout(new BorderLayout());

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);

    // Add picking behavior
    IntersectInfoBehavior behavior = new IntersectInfoBehavior(c, scene, 0.05f);
    behavior.setSchedulingBounds(bounds);
    scene.addChild(behavior);/*w  w  w .j a  v a 2 s. co  m*/

    TransformGroup vpTrans = u.getViewingPlatform().getViewPlatformTransform();

    KeyNavigatorBehavior keybehavior = new KeyNavigatorBehavior(vpTrans);
    keybehavior.setSchedulingBounds(bounds);
    scene.addChild(keybehavior);
    scene.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    scene.compile();
    u.addBranchGraph(scene);

    View view = u.getViewer().getView();
    view.setBackClipDistance(100000);

}

From source file:DoorApp.java

public DoorApp() {
    setLayout(new BorderLayout());
    Canvas3D canvas3D = new Canvas3D(null);
    add("Center", canvas3D);

    // SimpleUniverse is a Convenience Utility class
    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
    simpleU.getViewingPlatform().setNominalViewingTransform();

    BranchGroup scene = createSceneGraph();

    simpleU.addBranchGraph(scene);/*from  w  w  w. j a  v  a2 s .co  m*/
}

From source file:Text3DLoad.java

public void init() {

    if (textString == null) {
        textString = "Java3D";
    }/*  w  ww  . j a v a2s . co m*/
    setLayout(new BorderLayout());

    button = new Button("remove behaviors");
    button.addActionListener(this);
    Panel p = new Panel();
    p.add(button);
    add("South", p);

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();

    // create a SimpleUniverse with 4 TransformGroups for the mouse
    // behaviors
    u = new SimpleUniverse(c);

    // add the behaviors to the ViewingPlatform
    ViewingPlatform viewingPlatform = u.getViewingPlatform();

    viewingPlatform.setNominalViewingTransform();

    // add orbit behavior to ViewingPlatform
    orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL | OrbitBehavior.STOP_ZOOM);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    orbit.setSchedulingBounds(bounds);
    viewingPlatform.setViewPlatformBehavior(orbit);

    behaviorsOn = true;

    u.addBranchGraph(scene);
}

From source file:SoundBug.java

public void init() {

    // set up a NumFormat object to print out float with only 3 fraction
    // digits/*from w w w . j  a  v  a  2s.co m*/
    nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(3);

    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    canvas = null;
    canvas = new Canvas3D(config);
    add("Center", canvas);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(canvas);

    // set up sound
    u.getViewer().createAudioDevice();

    // get the view
    view = u.getViewer().getView();

    // Get the viewing platform
    ViewingPlatform viewingPlatform = u.getViewingPlatform();

    // Move the viewing platform back to enclose the -4 -> 4 range
    double viewRadius = 4.0; // want to be able to see circle
    // of viewRadius size around origin
    // get the field of view
    double fov = u.getViewer().getView().getFieldOfView();

    // calc view distance to make circle view in fov
    float viewDistance = (float) (viewRadius / Math.tan(fov / 2.0));
    tmpVector.set(0.0f, 0.0f, viewDistance);// setup offset
    tmpTrans.set(tmpVector); // set trans to translate
    // move the view platform
    viewingPlatform.getViewPlatformTransform().setTransform(tmpTrans);

    // add an orbit behavior to move the viewing platform
    OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.STOP_ZOOM);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    orbit.setSchedulingBounds(bounds);
    viewingPlatform.setViewPlatformBehavior(orbit);

    u.addBranchGraph(scene);

    add("South", soundPanel());
}

From source file:MultiTextureTest.java

public void init() {
    if (stoneImage == null) {
        // the path to the image for an applet
        try {/*from  ww  w.  j av  a 2  s .  c o m*/
            stoneImage = new java.net.URL(getCodeBase().toString() + "/stone.jpg");
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }

    if (skyImage == null) {
        // the path to the image for an applet
        try {
            skyImage = new java.net.URL(getCodeBase().toString() + "/bg.jpg");
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }

    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);

    ViewingPlatform viewingPlatform = u.getViewingPlatform();
    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    viewingPlatform.setNominalViewingTransform();

    // add orbit behavior but disable translate
    OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL | OrbitBehavior.DISABLE_TRANSLATE);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    orbit.setSchedulingBounds(bounds);
    viewingPlatform.setViewPlatformBehavior(orbit);

    u.addBranchGraph(scene);

    // create the gui
    choice = new Choice();
    choice.addItem("stone + light");
    choice.addItem("stone");
    choice.addItem("lightMap");
    choice.addItem("sky");
    choice.addItem("stone + sky");
    choice.addItemListener(this);
    add("North", choice);

}

From source file:cgview.java

public cgview(String args[]) {
    if (args.length < 1)
        usage();/*from w ww. j a  v  a2 s  . com*/

    int index;
    if (args.length < 2)
        index = 0;
    else
        index = Integer.parseInt(args[1]);

    String filename = args[0];
    if (filename == null)
        usage();

    // Read the compressed geometry.
    CompressedGeometry cg = null;
    try {
        CompressedGeometryFile cgf;
        cgf = new CompressedGeometryFile(filename, false);

        if (cgf.getObjectCount() == 0) {
            System.out.println("no objects were found in " + filename);
            System.exit(0);
        }

        cg = cgf.read(index);
        cgf.close();

    } catch (IOException e) {
        System.out.println(e);
        System.exit(0);
    }

    setLayout(new BorderLayout());
    Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph(cg);
    u = new SimpleUniverse(c);

    // add mouse behaviors to the ViewingPlatform
    ViewingPlatform viewingPlatform = u.getViewingPlatform();

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    viewingPlatform.setNominalViewingTransform();

    OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    orbit.setSchedulingBounds(bounds);
    viewingPlatform.setViewPlatformBehavior(orbit);

    u.addBranchGraph(scene);
}

From source file:ImageComponentByReferenceTest.java

public void init() {
    if (texImage == null) {
        // the path to the image for an applet
        try {/*from w  ww . j av  a2 s. c  om*/
            texImage = new java.net.URL(getCodeBase().toString() + "/one.jpg");
        } catch (java.net.MalformedURLException ex) {
            System.out.println(ex.getMessage());
            System.exit(1);
        }
    }

    Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);
    u.getViewingPlatform().setNominalViewingTransform();
    u.addBranchGraph(scene);
    Container contentPane = getContentPane();
    JPanel p = new JPanel();
    BoxLayout boxlayout = new BoxLayout(p, BoxLayout.Y_AXIS);
    p.setLayout(boxlayout);
    contentPane.add("Center", c);

    contentPane.add("South", p);

    p.add(createImagePanel());

}

From source file:PolygonOffset.java

public void init() {
    setLayout(new BorderLayout());

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    JPanel canvasPanel = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    canvasPanel.setLayout(gridbag);//from ww  w. j  a  v a2  s  . co m

    canvas = new Canvas3D(config);
    canvas.setSize(600, 600);
    add(canvas, BorderLayout.CENTER);

    u = new SimpleUniverse(canvas);

    if (isApplication) {
        offScreenCanvas = new OffScreenCanvas3D(config, true);
        // set the size of the off-screen canvas based on a scale
        // of the on-screen size
        Screen3D sOn = canvas.getScreen3D();
        Screen3D sOff = offScreenCanvas.getScreen3D();
        Dimension dim = sOn.getSize();
        dim.width *= offScreenScale;
        dim.height *= offScreenScale;
        sOff.setSize(dim);
        sOff.setPhysicalScreenWidth(sOn.getPhysicalScreenWidth() * offScreenScale);
        sOff.setPhysicalScreenHeight(sOn.getPhysicalScreenHeight() * offScreenScale);

        // attach the offscreen canvas to the view
        u.getViewer().getView().addCanvas3D(offScreenCanvas);
    }

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();

    // set the eye at z = 2.0
    viewingPlatform = u.getViewingPlatform();
    Transform3D vpTrans = new Transform3D();
    vpTrans.set(new Vector3f(0.0f, 0.0f, 2.0f));
    viewingPlatform.getViewPlatformTransform().setTransform(vpTrans);

    // set up a parallel projection with clip limits at 1 and -1
    view = u.getViewer().getView();
    view.setProjectionPolicy(View.PARALLEL_PROJECTION);
    view.setFrontClipPolicy(View.VIRTUAL_EYE);
    view.setBackClipPolicy(View.VIRTUAL_EYE);
    view.setFrontClipDistance(1.0f);
    view.setBackClipDistance(3.0f);

    u.addBranchGraph(scene);

    // set up the sliders
    JPanel guiPanel = new JPanel();
    guiPanel.setLayout(new GridLayout(0, 1));
    FloatLabelJSlider dynamicSlider = new FloatLabelJSlider("Dynamic Offset", 0.1f, 0.0f, 2.0f, dynamicOffset);
    dynamicSlider.addFloatListener(new FloatListener() {
        public void floatChanged(FloatEvent e) {
            dynamicOffset = e.getValue();
            solidPa.setPolygonOffsetFactor(dynamicOffset);
        }
    });
    guiPanel.add(dynamicSlider);

    LogFloatLabelJSlider staticSlider = new LogFloatLabelJSlider("Static Offset", 0.1f, 10000.0f, staticOffset);
    staticSlider.addFloatListener(new FloatListener() {
        public void floatChanged(FloatEvent e) {
            staticOffset = e.getValue();
            solidPa.setPolygonOffset(staticOffset);
        }
    });
    guiPanel.add(staticSlider);

    FloatLabelJSlider innerSphereSlider = new FloatLabelJSlider("Inner Sphere Scale", 0.001f, 0.90f, 1.0f,
            innerScale);
    innerSphereSlider.addFloatListener(new FloatListener() {
        public void floatChanged(FloatEvent e) {
            innerScale = e.getValue();
            updateInnerScale();
        }
    });
    guiPanel.add(innerSphereSlider);

    if (isApplication) {
        JButton snapButton = new JButton("Snap Image");
        snapButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Point loc = canvas.getLocationOnScreen();
                offScreenCanvas.setOffScreenLocation(loc);
                Dimension dim = canvas.getSize();
                dim.width *= offScreenScale;
                dim.height *= offScreenScale;
                nf.setMinimumIntegerDigits(3);
                offScreenCanvas.snapImageFile(outFileBase + nf.format(outFileSeq++), dim.width, dim.height);
                nf.setMinimumIntegerDigits(0);
            }
        });
        guiPanel.add(snapButton);
    }
    add(guiPanel, BorderLayout.EAST);
}