java left logo
java middle logo
java right logo
 

Home arrow Other API Tips arrow Java3D arrow Rendering an interactively rotatable traslatable and zoomable ColorCube object
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4095
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Rendering an interactively rotatable traslatable and zoomable ColorCube object E-mail
User Rating: / 3
PoorBest 

This Java tip renders a single, interactively rotatable, traslatable, and zoomable ColorCube object.


Image

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import com.sun.j3d.utils.applet.MainFrame; 
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;

import java.awt.event.*;
import java.util.Enumeration;

//   MouseBehaviorApp renders a single, interactively rotatable,
//   traslatable, and zoomable ColorCube object.

public class MouseBehaviorApp extends Applet {

    public BranchGroup createSceneGraph() {
  // Create the root of the branch graph
  BranchGroup objRoot = new BranchGroup();

        TransformGroup objTransform = new TransformGroup();
        objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);

        objRoot.addChild(objTransform);
        objTransform.addChild(new ColorCube(0.4));
        objRoot.addChild(new Axis());

        MouseRotate myMouseRotate = new MouseRotate();
        myMouseRotate.setTransformGroup(objTransform);
        myMouseRotate.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(myMouseRotate);

        MouseTranslate myMouseTranslate = new MouseTranslate();
        myMouseTranslate.setTransformGroup(objTransform);
        myMouseTranslate.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(myMouseTranslate);

        MouseZoom myMouseZoom = new MouseZoom();
        myMouseZoom.setTransformGroup(objTransform);
        myMouseZoom.setSchedulingBounds(new BoundingSphere());
        objRoot.addChild(myMouseZoom);

  // Let Java 3D perform optimizations on this scene graph.
        objRoot.compile();

  return objRoot;
    // end of CreateSceneGraph method of MouseBehaviorApp

    // Create a simple scene and attach it to the virtual universe

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

        BranchGroup scene = createSceneGraph();

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

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

        simpleU.addBranchGraph(scene);
    // end of MouseBehaviorApp (constructor)
    //  The following allows this to be run as an application
    //  as well as an applet

    public static void main(String[] args) {
        System.out.print("MouseBehaviorApp.java \n- a demonstration of using the mouse ");
        System.out.println("behavior utility classes to provide interaction in a Java 3D scene.");
        System.out.println("Hold the mouse button while moving the mouse to make the cube move.");
        System.out.println("     left mouse button      - rotate cube");
        System.out.println("     right mouse button     - translate cube");
        System.out.println("     Alt+left mouse button  - zoom cube");
        System.out.println("This is a simple example progam from The Java 3D API Tutorial.");
        System.out.println("The Java 3D Tutorial is available on the web at:");
        System.out.println("http://www.sun.com/desktop/java3d/collateral");
        Frame frame = new MainFrame(new MouseBehaviorApp()256256);
    // end of main (method of MouseBehaviorApp)

// end of class MouseBehaviorApp


/*
 * Getting Started with the Java 3D API written in Java 3D
 
 * This program demonstrates: 1. writing a visual object class In this program,
 * Axis class defines a visual object This particular class extends Shape3D See
 * the text for a discussion. 2. Using LineArray to draw 3D lines.
 */

class Axis extends Shape3D {

  ////////////////////////////////////////////
  //
  // create axis visual object
  //
  public Axis() {

    this.setGeometry(createGeometry());

  }

  private Geometry createGeometry() {
    // create line for X axis
    IndexedLineArray axisLines = new IndexedLineArray(18,
        GeometryArray.COORDINATES, 30);

    axisLines.setCoordinate(0new Point3f(-1.0f0.0f0.0f));
    axisLines.setCoordinate(1new Point3f(1.0f0.0f0.0f));
    axisLines.setCoordinate(2new Point3f(0.9f0.1f0.1f));
    axisLines.setCoordinate(3new Point3f(0.9f, -0.1f0.1f));
    axisLines.setCoordinate(4new Point3f(0.9f0.1f, -0.1f));
    axisLines.setCoordinate(5new Point3f(0.9f, -0.1f, -0.1f));
    axisLines.setCoordinate(6new Point3f(0.0f, -1.0f0.0f));
    axisLines.setCoordinate(7new Point3f(0.0f1.0f0.0f));
    axisLines.setCoordinate(8new Point3f(0.1f0.9f0.1f));
    axisLines.setCoordinate(9new Point3f(-0.1f0.9f0.1f));
    axisLines.setCoordinate(10new Point3f(0.1f0.9f, -0.1f));
    axisLines.setCoordinate(11new Point3f(-0.1f0.9f, -0.1f));
    axisLines.setCoordinate(12new Point3f(0.0f0.0f, -1.0f));
    axisLines.setCoordinate(13new Point3f(0.0f0.0f1.0f));
    axisLines.setCoordinate(14new Point3f(0.1f0.1f0.9f));
    axisLines.setCoordinate(15new Point3f(-0.1f0.1f0.9f));
    axisLines.setCoordinate(16new Point3f(0.1f, -0.1f0.9f));
    axisLines.setCoordinate(17new Point3f(-0.1f, -0.1f0.9f));

    axisLines.setCoordinateIndex(00);
    axisLines.setCoordinateIndex(11);
    axisLines.setCoordinateIndex(22);
    axisLines.setCoordinateIndex(31);
    axisLines.setCoordinateIndex(43);
    axisLines.setCoordinateIndex(51);
    axisLines.setCoordinateIndex(64);
    axisLines.setCoordinateIndex(71);
    axisLines.setCoordinateIndex(85);
    axisLines.setCoordinateIndex(91);
    axisLines.setCoordinateIndex(106);
    axisLines.setCoordinateIndex(117);
    axisLines.setCoordinateIndex(128);
    axisLines.setCoordinateIndex(137);
    axisLines.setCoordinateIndex(149);
    axisLines.setCoordinateIndex(157);
    axisLines.setCoordinateIndex(1610);
    axisLines.setCoordinateIndex(177);
    axisLines.setCoordinateIndex(1811);
    axisLines.setCoordinateIndex(197);
    axisLines.setCoordinateIndex(2012);
    axisLines.setCoordinateIndex(2113);
    axisLines.setCoordinateIndex(2214);
    axisLines.setCoordinateIndex(2313);
    axisLines.setCoordinateIndex(2415);
    axisLines.setCoordinateIndex(2513);
    axisLines.setCoordinateIndex(2616);
    axisLines.setCoordinateIndex(2713);
    axisLines.setCoordinateIndex(2817);
    axisLines.setCoordinateIndex(2913);return axisLines;

  // end of Axis createGeometry()

// end of class Axis

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.