此程序使用AWT按钮,使用户能够旋转物体 : 三维变换 « 三维图形动画 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 三维图形动画 » 三维变换屏幕截图 
此程序使用AWT按钮,使用户能够旋转物体
此程序使用AWT按钮,使用户能够旋转物体


/*
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.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.Node;
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.AxisAngle4d;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.Box;

/**
 * This program uses AWT buttons to allow the user to rotate an object. This is
 * achieved by altering the transform of a transform group.
 
 @author I.J.Palmer
 @version 1.0
 */
public class SimpleTransform extends Frame implements ActionListener {
  protected Canvas3D myCanvas3D = new Canvas3D(null);

  /** The exit button */
  protected Button exitButton = new Button("Exit");

  /** The rotate left button */
  protected Button leftButton = new Button("<-");

  /** The rotate right button */
  protected Button rightButton = new Button("->");

  /** The transform group used to rotate the shape */
  protected TransformGroup rotationGroup;

  /**
   * This function builds the view branch of the scene graph. It creates a
   * branch group and then creates the necessary view elements to give a
   * useful view of our content.
   
   @param c
   *            Canvas3D that will display the view
   @return BranchGroup that is the root of the view elements
   */
  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);
    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 so that we can illuminate the scene. This adds one
   * ambient light to bring up the overall lighting level and one directional
   * shape to show the shape of the objects in the scene.
   
   @param b
   *            BranchGroup that the lights are to be added to.
   */
  protected void addLights(BranchGroup b) {
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.00.00.0),
        100.0);
    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);
  }

  /**
   * This builds the content branch of our scene graph. The root of the shapes
   * supplied as a parameter is slightly tilted to reveal its 3D shape. It
   * also uses the addLights function to add some lights to the scene. The
   * group that the shape is added to has its capabilities set so that we can
   * read and write it.
   
   @param shape
   *            Node that represents the geometry for the content
   @return BranchGroup that is the root of the content branch
   */
  protected BranchGroup buildContentBranch(Node shape) {
    BranchGroup contentBranch = new BranchGroup();
    Transform3D rotateCube = new Transform3D();
    rotateCube.set(new AxisAngle4d(1.01.00.0, Math.PI / 4.0));
    rotationGroup = new TransformGroup(rotateCube);
    //Set the capabilities so that the transform can be accessed
    rotationGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    rotationGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    contentBranch.addChild(rotationGroup);
    rotationGroup.addChild(shape);
    addLights(contentBranch);
    return contentBranch;
  }

  /**
   * This defines the shapes used in the scene. It creates a simple cube using
   * a Box utility class.
   
   @return Node that is the root of the shape hierarchy.
   */
  protected Node buildShape() {
    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));
    return new Box(2.0f2.0f2.0f, app);
  }

  /**
   * This processes the AWT events and performs the appropriate operations.
   * The exit button causes the program to terminate, the left button causes a
   * rotation to be applied to the shape's transformation to spin it to the
   * left and the right has the similar effect but to the right button.
   
   @param e
   *            ActionEvent that has been performed
   */
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == exitButton) {
      dispose();
      System.exit(0);
    else if (e.getSource() == leftButton) {
      //Create a temporary transform
      Transform3D temp = new Transform3D();
      //Read the transform from the shape
      rotationGroup.getTransform(temp);
      //Create a rotation that will be applied
      Transform3D tempDelta = new Transform3D();
      tempDelta.rotY(-0.3);
      //Apply the rotation
      temp.mul(tempDelta);
      //Write the value back into the scene graph
      rotationGroup.setTransform(temp);
    else if (e.getSource() == rightButton) {
      //Do the same for the right rotation
      Transform3D temp = new Transform3D();
      rotationGroup.getTransform(temp);
      Transform3D tempDelta = new Transform3D();
      tempDelta.rotY(0.3);
      temp.mul(tempDelta);
      rotationGroup.setTransform(temp);
    }
  }

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

  public static void main(String[] args) {
    SimpleTransform st = new SimpleTransform();
  }
}


           
       
Related examples in the same category
1. ExTransform -使用变换ExTransform -使用变换
2. AWT互动:变换三维AWT互动:变换三维
3. 变换和光变换和光
4. Transform ExplorerTransform Explorer
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.