This uses the class SimpleMorphBehaviour to animate : 3D Animation « 3D « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » 3D » 3D AnimationScreenshots 
This uses the class SimpleMorphBehaviour to animate
This uses the class SimpleMorphBehaviour to animate

/*
Essential Java 3D Fast

Ian Palmer

Publisher: Springer-Verlag

ISBN: 1-85233-394-4

*/

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Enumeration;

import javax.media.j3d.Alpha;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Behavior;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.IndexedQuadArray;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
import javax.media.j3d.Morph;
import javax.media.j3d.PhysicalBody;
import javax.media.j3d.PhysicalEnvironment;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.media.j3d.ViewPlatform;
import javax.media.j3d.VirtualUniverse;
import javax.media.j3d.WakeupCriterion;
import javax.media.j3d.WakeupOnAWTEvent;
import javax.media.j3d.WakeupOnElapsedFrames;
import javax.media.j3d.WakeupOr;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;

/**
 * This uses the class SimpleMorphBehaviour to animate a shape between two key
 * shapes: a cube and a pyramid. The Morph object is the same as that used in
 * the program SimpleMorph, but this time we use an alpha generator to drive the
 * animation.
 
 @author I.J.Palmer
 @version 1.0
 @see SimpleMorphBehaviour
 @see SimpleMorph
 */
public class SimpleMorph2 extends Frame implements ActionListener {
  protected Canvas3D myCanvas3D = new Canvas3D(null);

  protected Button exitButton = new Button("Exit");

  /** This performs the animation */
  protected Morph myMorph;

  /** This drives the Morph object */
  protected SimpleMorphBehaviour myBehave;

  /** The active bounds for the behaviour */
  protected BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.0,
      0.0)100.0);

  /**
   * Build the view branch of the scene graph
   
   @return BranchGroup that is the root of the view branch
   */
  protected BranchGroup buildViewBranch(Canvas3D c) {
    BranchGroup viewBranch = new BranchGroup();
    Transform3D viewXfm = new Transform3D();
    viewXfm.set(new Vector3f(0.0f0.0f5.0f));
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    ViewPlatform myViewPlatform = new ViewPlatform();
    PhysicalBody myBody = new PhysicalBody();
    PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
    viewXfmGroup.addChild(myViewPlatform);
    viewBranch.addChild(viewXfmGroup);
    View myView = new View();
    myView.addCanvas3D(c);
    myView.attachViewPlatform(myViewPlatform);
    myView.setPhysicalBody(myBody);
    myView.setPhysicalEnvironment(myEnvironment);
    return viewBranch;
  }

  /**
   * Add some lights to the scene graph
   
   @param b
   *            BranchGroup that the lights are added to
   */
  protected void addLights(BranchGroup b) {
    Color3f ambLightColour = new Color3f(0.5f0.5f0.5f);
    AmbientLight ambLight = new AmbientLight(ambLightColour);
    ambLight.setInfluencingBounds(bounds);
    Color3f dirLightColour = new Color3f(1.0f1.0f1.0f);
    Vector3f dirLightDir = new Vector3f(-1.0f, -1.0f, -1.0f);
    DirectionalLight dirLight = new DirectionalLight(dirLightColour,
        dirLightDir);
    dirLight.setInfluencingBounds(bounds);
    b.addChild(ambLight);
    b.addChild(dirLight);
  }

  /**
   * Create the Morph from the given shapes
   
   @param theShapes
   *            GeometryArray that stores the shapes for the Morph
   @param app
   *            Appearnce used for the shapes
   @return Morph that uses the given shapes
   */
  protected Morph createMorph(GeometryArray[] theShapes, Appearance app) {
    double[] weights = 1.00.0 };
    Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
        | Alpha.DECREASING_ENABLE, 00400020000400020000);
    myMorph = new Morph(theShapes, app);
    myMorph.setWeights(weights);
    myMorph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
    myBehave = new SimpleMorphBehaviour(morphAlpha, myMorph);
    myBehave.setSchedulingBounds(bounds);
    return myMorph;
  }

  /**
   * Build the content branch for the scene graph
   
   @return BranchGroup that is the root of the content
   */
  protected BranchGroup buildContentBranch() {
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f0.0f0.0f);
    Color3f emissiveColour = new Color3f(0.0f0.0f0.0f);
    Color3f specularColour = new Color3f(1.0f1.0f1.0f);
    Color3f diffuseColour = new Color3f(1.0f0.0f0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour,
        diffuseColour, specularColour, shininess));
    //Make the cube key shape
    IndexedQuadArray indexedCube = new IndexedQuadArray(8,
        IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    Point3f[] cubeCoordinates = new Point3f(1.0f1.0f1.0f),
        new Point3f(-1.0f1.0f1.0f),
        new Point3f(-1.0f, -1.0f1.0f),
        new Point3f(1.0f, -1.0f1.0f)new Point3f(1.0f1.0f, -1.0f),
        new Point3f(-1.0f1.0f, -1.0f),
        new Point3f(-1.0f, -1.0f, -1.0f),
        new Point3f(1.0f, -1.0f, -1.0f) };
    Vector3f[] cubeNormals = new Vector3f(0.0f0.0f1.0f),
        new Vector3f(0.0f0.0f, -1.0f),
        new Vector3f(1.0f0.0f0.0f),
        new Vector3f(-1.0f0.0f0.0f),
        new Vector3f(0.0f1.0f0.0f)new Vector3f(0.0f, -1.0f0.0f) };
    int cubeCoordIndices[] 012376540374562,
        10451673};
    int cubeNormalIndices[] 00001111222233,
        334444555};
    indexedCube.setCoordinates(0, cubeCoordinates);
    indexedCube.setNormals(0, cubeNormals);
    indexedCube.setCoordinateIndices(0, cubeCoordIndices);
    indexedCube.setNormalIndices(0, cubeNormalIndices);

    //Make the pyramid key shape. Although this needs
    //only five vertices to create the desired shape, we
    //need to use six vertices so that it has the same
    //number as the cube.
    IndexedQuadArray indexedPyramid = new IndexedQuadArray(8,
        IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24);
    Point3f[] pyramidCoordinates = new Point3f(0.0f1.0f0.0f),
        new Point3f(0.0f1.0f0.0f)new Point3f(-1.0f, -1.0f1.0f),
        new Point3f(1.0f, -1.0f1.0f)new Point3f(0.0f1.0f0.0f),
        new Point3f(0.0f1.0f0.0f),
        new Point3f(-1.0f, -1.0f, -1.0f),
        new Point3f(1.0f, -1.0f, -1.0f) };
    Vector3f[] pyramidNormals = new Vector3f(0.0f0.0f1.0f),
        new Vector3f(0.0f0.0f, -1.0f),
        new Vector3f(1.0f0.0f0.0f),
        new Vector3f(-1.0f0.0f0.0f),
        new Vector3f(0.0f1.0f0.0f)new Vector3f(0.0f, -1.0f0.0f) };
    int pyramidCoordIndices[] 01237654037456,
        210451673};
    int pyramidNormalIndices[] 0000111122223,
        3334444555};
    indexedPyramid.setCoordinates(0, pyramidCoordinates);
    indexedPyramid.setNormals(0, pyramidNormals);
    indexedPyramid.setCoordinateIndices(0, pyramidCoordIndices);
    indexedPyramid.setNormalIndices(0, pyramidNormalIndices);
    //Set the contents of the array to the two shapes
    GeometryArray[] theShapes = new GeometryArray[2];
    theShapes[0= indexedCube;
    theShapes[1= indexedPyramid;
    BranchGroup contentBranch = new BranchGroup();
    //Create a transform to rotate the shape slightly
    Transform3D rotateCube = new Transform3D();
    rotateCube.set(new AxisAngle4d(1.01.00.0, Math.PI / 4.0));
    TransformGroup rotationGroup = new TransformGroup(rotateCube);
    contentBranch.addChild(rotationGroup);
    addLights(contentBranch);
    //Call the function to build the morph
    rotationGroup.addChild(createMorph(theShapes, app));
    //Add the behaviour to the scene graph to activate it
    rotationGroup.addChild(myBehave);
    return contentBranch;

  }

  public void actionPerformed(ActionEvent e) {
    dispose();
    System.exit(0);
  }

  public SimpleMorph2() {
    VirtualUniverse myUniverse = new VirtualUniverse();
    Locale myLocale = new Locale(myUniverse);
    myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
    myLocale.addBranchGraph(buildContentBranch());
    setTitle("SimpleMorph");
    setSize(400400);
    setLayout(new BorderLayout());
    Panel bottom = new Panel();
    bottom.add(exitButton);
    add(BorderLayout.CENTER, myCanvas3D);
    add(BorderLayout.SOUTH, bottom);
    exitButton.addActionListener(this);
    setVisible(true);
  }

  public static void main(String[] args) {
    SimpleMorph2 sw = new SimpleMorph2();
  }
}

/**
 * This class uses an alpha generator to change the weights of a Morph node. It
 * morphs a shape between two key shapes repeatedly once a key has been pressed.
 * Subsequent key presses toggle the running state of the animation.
 
 @author I.J.Palmer
 @version 1.0
 @see SimpleMorph2
 */

class SimpleMorphBehaviour extends Behavior {
  /** Used to drive the animation */
  protected Alpha theAlpha;

  /** The weights of this are changed by the alpha values */
  protected Morph theMorph;

  /** Used to define the Morph weights */
  protected double theWeights[] new double[2];

  /** Defines whether the animation is running or not */
  protected boolean running = false;

  /** The triggers for the animation to start */
  protected WakeupCriterion[] wakeConditions;

  /** The combined triggers */
  protected WakeupOr oredConditions;

  /** Set up the criteria to trigger after zero time or when a key is pressed */
  public void initialize() {
    wakeConditions = new WakeupCriterion[2];
    wakeConditions[0new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
    wakeConditions[1new WakeupOnElapsedFrames(0);
    oredConditions = new WakeupOr(wakeConditions);
    wakeupOn(wakeConditions[0]);
  }

  /**
   * If the behaviour is not running and and a key has been pressed, start the
   * animation and vice-versa. Otherwise calculate a new set of weights.
   */
  public void processStimulus(Enumeration criteria) {
    WakeupCriterion theCriteria;
    theCriteria = (WakeupCriterioncriteria.nextElement();
    //If a key has been pressed, toggle the running state
    if (theCriteria instanceof WakeupOnAWTEvent) {
      running = !running;
    }
    if (running) {
      //Get the alpha value
      double alphaValue = theAlpha.value();
      //Set the two weights according to this value
      theWeights[01.0 - alphaValue;
      theWeights[1= alphaValue;
      //Use the weights in the Morph
      theMorph.setWeights(theWeights);
    }
    //Set the trigger conditions again
    wakeupOn(oredConditions);
  }

  /**
   * Set up the data for the behaviour.
   
   @param a
   *            Alpha that is used to drive the animation
   @param m
   *            Morph that is affected by this behaviour
   */
  public SimpleMorphBehaviour(Alpha a, Morph m) {
    theAlpha = a;
    theMorph = m;
  }
}



           
       
Related examples in the same category
1. Human AnimationHuman Animation
2. This example creates a 3D fly-over of the city of BostonThis example creates a 3D fly-over of the city of Boston
3. Simple DOOM style navigation of a 3D scene using Java 3DSimple DOOM style navigation of a 3D scene using Java 3D
4. HelloJava3Da renders a single, rotating cubeHelloJava3Da renders a single, rotating cube
5. Spline AnimationSpline Animation
6. Animation and Interaction - a Bouncing BallAnimation and Interaction - a Bouncing Ball
w___w___w.ja_va2_s___.___co___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.