java left logo
java middle logo
java right logo
 

Home arrow Other API Tips
 
 
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: 4086
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:
 
 
 
How to render a simple landscape E-mail
User Rating: / 2
PoorBest 

This Java tip renders a simple landscape.


Image

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.IndexedTriangleArray;
import javax.media.j3d.LineArray;
import javax.media.j3d.Link;
import javax.media.j3d.Shape3D;
import javax.media.j3d.SharedGroup;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.universe.SimpleUniverse;

//   KeyNavigatorApp renders a simple landscape

public class KeyNavigatorApp extends Applet {

  Shape3D createPyramid() {
    IndexedTriangleArray pyGeom = new IndexedTriangleArray(5,
        GeometryArray.COORDINATES | GeometryArray.COLOR_3, 12);

    pyGeom.setCoordinate(0new Point3f(0.0f0.7f0.0f));
    pyGeom.setCoordinate(1new Point3f(-0.4f0.0f, -0.4f));
    pyGeom.setCoordinate(2new Point3f(-0.4f0.0f0.4f));
    pyGeom.setCoordinate(3new Point3f(0.4f0.0f0.4f));
    pyGeom.setCoordinate(4new Point3f(0.4f0.0f, -0.4f));

    pyGeom.setCoordinateIndex(00);
    pyGeom.setCoordinateIndex(11);
    pyGeom.setCoordinateIndex(22);
    pyGeom.setCoordinateIndex(30);
    pyGeom.setCoordinateIndex(42);
    pyGeom.setCoordinateIndex(53);
    pyGeom.setCoordinateIndex(60);
    pyGeom.setCoordinateIndex(73);
    pyGeom.setCoordinateIndex(84);
    pyGeom.setCoordinateIndex(90);
    pyGeom.setCoordinateIndex(104);
    pyGeom.setCoordinateIndex(111);

    Color3f c = new Color3f(0.6f0.5f0.55f);
    pyGeom.setColor(0, c);
    pyGeom.setColor(1, c);
    pyGeom.setColor(2, c);
    pyGeom.setColor(3, c);
    pyGeom.setColor(4, c);

    Shape3D pyramid = new Shape3D(pyGeom);
    return pyramid;
  }

  Shape3D createLand() {
    LineArray landGeom = new LineArray(44, GeometryArray.COORDINATES
        | GeometryArray.COLOR_3);
    float l = -50.0f;
    for (int c = 0; c < 44; c += 4) {

      landGeom.setCoordinate(c + 0new Point3f(-50.0f0.0f, l));
      landGeom.setCoordinate(c + 1new Point3f(50.0f0.0f, l));
      landGeom.setCoordinate(c + 2new Point3f(l, 0.0f, -50.0f));
      landGeom.setCoordinate(c + 3new Point3f(l, 0.0f50.0f));
      l += 10.0f;
    }

    Color3f c = new Color3f(0.1f0.8f0.1f);
    for (int i = 0; i < 44; i++)
      landGeom.setColor(i, c);

    return new Shape3D(landGeom);
  }

  public BranchGroup createSceneGraph(SimpleUniverse su) {
    // Create the root of the branch graph
    TransformGroup vpTrans = null;

    BranchGroup objRoot = new BranchGroup();

    Vector3f translate = new Vector3f();
    Transform3D T3D = new Transform3D();
    TransformGroup TG = null;

    objRoot.addChild(createLand());

    SharedGroup share = new SharedGroup();
    share.addChild(createPyramid());

    float[][] position = { { 0.0f0.0f, -3.0f }6.0f0.0f0.0f },
        6.0f0.0f6.0f }3.0f0.0f, -10.0f },
        13.0f0.0f, -30.0f }-13.0f0.0f30.0f },
        -13.0f0.0f23.0f }13.0f0.0f3.0f } };

    for (int i = 0; i < position.length; i++) {
      translate.set(position[i]);
      T3D.setTranslation(translate);
      TG = new TransformGroup(T3D);
      TG.addChild(new Link(share));
      objRoot.addChild(TG);
    }
    vpTrans = su.getViewingPlatform().getViewPlatformTransform();
    translate.set(0.0f0.3f0.0f);
    T3D.setTranslation(translate);
    vpTrans.setTransform(T3D);
    KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans);
    keyNavBeh
        .setSchedulingBounds(new BoundingSphere(new Point3d()1000.0));
    objRoot.addChild(keyNavBeh);

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

    return objRoot;
  // end of CreateSceneGraph method of KeyNavigatorApp

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

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

    BranchGroup scene = createSceneGraph(simpleU);

    simpleU.addBranchGraph(scene);
  // end of KeyNavigatorApp (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("KeyNavigatorApp.java \n- a demonstration of the KeyNavigatorBehavior ");
    System.out
        .println("class to provide keyboard navigation in a Java 3D scene.");
    System.out
        .println("When the app loads, you can use the arrow keys to move.");
    System.out
        .println("This is an 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 KeyNavigatorApp()256256);
  // end of main (method of KeyNavigatorApp)

// end of class KeyNavigatorApp

 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.