This application demonstrates the use of a billboard node : Mouse Keyboard Action « 3D « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 3D » Mouse Keyboard ActionScreenshots 
This application demonstrates the use of a billboard node
This application demonstrates the use of a billboard node

/*
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 javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Billboard;
import javax.media.j3d.BoundingLeaf;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Locale;
import javax.media.j3d.Material;
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.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.ColorCube;

/**
 * This application demonstrates the use of a billboard node. Two cubes are
 * created, one yellow and the other with a a different colour on each face. The
 * billboard is created so that the red face of the right cube is always facing
 * the viewer. A key board utility class is used to allow modification of the
 * view angle.
 
 @author I.J.Palmer
 @version 1.0
 */
public class SimpleBillboard extends Frame implements ActionListener {
  protected Canvas3D myCanvas3D = new Canvas3D(null);

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

  protected BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.0,
      0.0)100.0);

  /**
   * Build the view branch of the scene graph. In this case a key navigation
   * utility object is created and associated with the view transform so that
   * the view can be changed via the keyboard.
   
   @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.0f10.0f));
    TransformGroup viewXfmGroup = new TransformGroup(viewXfm);
    viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    viewXfmGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    BoundingSphere movingBounds = new BoundingSphere(new Point3d(0.00.0,
        0.0)100.0);
    BoundingLeaf boundLeaf = new BoundingLeaf(movingBounds);
    ViewPlatform myViewPlatform = new ViewPlatform();
    viewXfmGroup.addChild(boundLeaf);
    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);

    KeyNavigatorBehavior keyNav = new KeyNavigatorBehavior(viewXfmGroup);
    keyNav.setSchedulingBounds(movingBounds);
    viewBranch.addChild(keyNav);

    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);
  }

  /**
   * Build the content branch for the scene graph. This creates two cubes and
   * uses a billboard node to keep one face of one of the cubes facing the
   * viewer.
   
   @return BranchGroup that is the root of the content
   */
  protected BranchGroup buildContentBranch() {
    //Create the appearance
    Appearance app = new Appearance();
    Color3f ambientColour = new Color3f(1.0f1.0f0.0f);
    Color3f emissiveColour = new Color3f(0.0f0.0f0.0f);
    Color3f specularColour = new Color3f(1.0f1.0f1.0f);
    Color3f diffuseColour = new Color3f(1.0f1.0f0.0f);
    float shininess = 20.0f;
    app.setMaterial(new Material(ambientColour, emissiveColour,
        diffuseColour, specularColour, shininess));
    //Make the cubes
    Box leftCube = new Box(1.0f1.0f1.0f, app);
    ColorCube rightCube = new ColorCube();
    //Create the transformgroup used for the billboard
    TransformGroup billBoardGroup = new TransformGroup();
    //Set the access rights to the group
    billBoardGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    //Add the cube to the group
    billBoardGroup.addChild(rightCube);
    //Create and activate the billboard
    Billboard myBillboard = new Billboard(billBoardGroup,
        Billboard.ROTATE_ABOUT_AXIS, new Vector3f(0.0f1.0f0.0f));
    myBillboard.setSchedulingBounds(bounds);
    BranchGroup contentBranch = new BranchGroup();
    contentBranch.addChild(myBillboard);
    addLights(contentBranch);
    //Position the cubes
    TransformGroup bothGroup = new TransformGroup();
    Transform3D leftGroupXfm = new Transform3D();
    leftGroupXfm.set(new Vector3d(-1.50.00.0));
    TransformGroup leftGroup = new TransformGroup(leftGroupXfm);
    Transform3D rightGroupXfm = new Transform3D();
    rightGroupXfm.set(new Vector3d(1.50.00.0));
    TransformGroup rightGroup = new TransformGroup(rightGroupXfm);
    //Put it all together
    bothGroup.addChild(leftGroup);
    leftGroup.addChild(leftCube);
    bothGroup.addChild(rightGroup);
    rightGroup.addChild(billBoardGroup);
    contentBranch.addChild(bothGroup);
    return contentBranch;

  }

  /**
   * Use the action event of the exit button to end the application.
   */
  public void actionPerformed(ActionEvent e) {
    dispose();
    System.exit(0);
  }

  public SimpleBillboard() {
    VirtualUniverse myUniverse = new VirtualUniverse();
    Locale myLocale = new Locale(myUniverse);
    myLocale.addBranchGraph(buildViewBranch(myCanvas3D));
    myLocale.addBranchGraph(buildContentBranch());
    setTitle("SimpleBillboard");
    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) {
    SimpleBillboard sw = new SimpleBillboard();
  }
}           
       
Related examples in the same category
1. Navigation around the sceneNavigation around the scene
2. This is our first simple program that creates a cubeThis is our first simple program that creates a cube
3. Class Drag
4. Picking utilities on various GeometryArray subclasses and Morph objectPicking utilities on various GeometryArray subclasses and Morph object
5. Oriented TestOriented Test
6. Oriented Pt TestOriented Pt Test
7. Improved mouse behaviorsImproved mouse behaviors
8. Demonstrate the use of the mouse utility classesDemonstrate the use of the mouse utility classes
9. MouseBehaviorApp renders a single, interactively rotatable,translatable, and zoomable ColorCube objectMouseBehaviorApp renders a single, interactively rotatable,translatable, and zoomable ColorCube object
10. MouseRotate2App renders a single, interactively rotatable cubeMouseRotate2App renders a single, interactively rotatable cube
11. MouseNavigatorApp renders a single, interactively rotatable, traslatable, and zoomable ColorCube objectMouseNavigatorApp renders a single, interactively rotatable, traslatable, and zoomable ColorCube object
12. KeyNavigatorApp renders a simple landscapeKeyNavigatorApp renders a simple landscape
13. Rotate when any key is pressedRotate when any key is pressed
14. PickCallbackApp renders two interactively rotatable cubesPickCallbackApp renders two interactively rotatable cubes
15. MouseRotateApp renders a single, interactively rotatable cubeMouseRotateApp renders a single, interactively rotatable cube
16. MousePickApp renders two interactively rotatable cubesMousePickApp renders two interactively rotatable cubes
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.