⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j3dapplet.java~140~

📁 这是本人刚刚得到的JBuilder9软件开发项目实践的源码
💻 JAVA~140~
📖 第 1 页 / 共 2 页
字号:
package j3ddemo;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.TextureLoader;
/**
 * <p>Title: Java 3D Demo</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Sttony
 * @version 1.0
 */

public class J3DApplet extends Applet {
  private boolean isStandalone = false;
  XYLayout xYLayout1 = new XYLayout();
  JTextField jTextField_txtString = new JTextField();
  JComboBox jComboBox_TopLeft = new JComboBox();
  JButton jButton_ChangText = new JButton();
  JPanel jPanel1 = new JPanel();
  XYLayout xYLayout2 = new XYLayout();
  JPanel jPanel2 = new JPanel();
  XYLayout xYLayout3 = new XYLayout();
  JComboBox jComboBox_TopRight = new JComboBox();
  JComboBox jComboBox_ButtomLeft = new JComboBox();
  JComboBox jComboBox_ButtomRight = new JComboBox();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JLabel jLabel3 = new JLabel();
  JLabel jLabel4 = new JLabel();
  JPanel jPanel4 = new JPanel();
  XYLayout xYLayout5 = new XYLayout();
  JSlider jSlider2 = new JSlider();
  JLabel jLabel6 = new JLabel();

  // 自己所用的参数
  String txtString; // 要显示的字符串
  int fD=4000;        //  旋转周期,文字为1倍,几何体为1.5倍
  static String strGeometryType_Cube="立方体";
  static String strGeometryType_Cone="圆锥";
  static String strGeometryType_Prim="棱锥";
  static String strGeometryType_withTex="带纹理形体";
  static String strGeometryType_Text3D="3D文字";
  private SimpleUniverse u = null;  //虚拟世界
  private BranchGroup scence=null;      //  场景图的根
  private int ibgType[]= new int [4];           // 存储4个角图元类型的数组
  private Group bgTopLeft=null;   //左上角
  private Group bgTopRight=null;   //右上角
  private Group bgButtomLeft=null;  //左下角
  private Group bgButtomRight=null;   //右下角
  private Group bgCenterText=null;   //中央文字

  //Get a parameter value
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  //Construct the applet
  public J3DApplet() {
  }
  //Initialize the applet
  public void init() {
    try {
      txtString = this.getParameter("Text", "Java 3D");
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception {
    this.setLayout(xYLayout1);
    xYLayout1.setWidth(600);
    xYLayout1.setHeight(500);
    jTextField_txtString.setText("Java 3D");
    jComboBox_TopLeft.setMinimumSize(new Dimension(27, 22));
    jComboBox_TopLeft.setToolTipText("");
    jComboBox_TopLeft.setVerifyInputWhenFocusTarget(true);
    jComboBox_TopLeft.setLightWeightPopupEnabled(true);
    jComboBox_TopLeft.addItemListener(new J3DApplet_jComboBox_TopLeft_itemAdapter(this));
    jButton_ChangText.setFont(new java.awt.Font("Dialog", 0, 12));
    jButton_ChangText.setText("更改文字");
    jButton_ChangText.addActionListener(new J3DApplet_jButton_ChangText_actionAdapter(this));
    jPanel1.setBorder(BorderFactory.createEtchedBorder());
    jPanel1.setLayout(xYLayout2);
    jPanel2.setLayout(xYLayout3);
    jPanel2.setBorder(BorderFactory.createEtchedBorder());
    jComboBox_TopRight.setLightWeightPopupEnabled(true);
    jComboBox_TopRight.setSelectedIndex(-1);
    jComboBox_TopRight.addItemListener(new J3DApplet_jComboBox_TopRight_itemAdapter(this));
    jComboBox_TopRight.setVerifyInputWhenFocusTarget(true);
    jComboBox_TopRight.setToolTipText("");
    jComboBox_TopRight.setMinimumSize(new Dimension(27, 22));
    jComboBox_ButtomLeft.setMinimumSize(new Dimension(27, 22));
    jComboBox_ButtomLeft.setToolTipText("");
    jComboBox_ButtomLeft.setVerifyInputWhenFocusTarget(true);
    jComboBox_ButtomLeft.setLightWeightPopupEnabled(true);
    jComboBox_ButtomLeft.addItemListener(new J3DApplet_jComboBox_ButtomLeft_itemAdapter(this));
    jComboBox_ButtomRight.setLightWeightPopupEnabled(true);
    jComboBox_ButtomRight.addItemListener(new J3DApplet_jComboBox_ButtomRight_itemAdapter(this));
    jComboBox_ButtomRight.setVerifyInputWhenFocusTarget(true);
    jComboBox_ButtomRight.setToolTipText("");
    jComboBox_ButtomRight.setMinimumSize(new Dimension(27, 22));
    jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel1.setText("左上角");
    jLabel2.setText("右上角");
    jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel3.setText("左下角");
    jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel4.setText("右上角");
    jLabel4.setFont(new java.awt.Font("Dialog", 0, 12));
    jPanel4.setLayout(xYLayout5);
    jPanel4.setBorder(BorderFactory.createEtchedBorder());
    jLabel6.setFont(new java.awt.Font("Dialog", 0, 12));
    jLabel6.setText("- 调节旋转速度 +");
    jSlider2.addMouseListener(new J3DApplet_jSlider2_mouseAdapter(this));
    jSlider2.addMouseMotionListener(new J3DApplet_jSlider2_mouseMotionAdapter(this));
    jSlider2.addKeyListener(new J3DApplet_jSlider2_keyAdapter(this));
    this.add(jPanel1,     new XYConstraints(14, 418, 119, 71));
    jPanel1.add(jTextField_txtString,     new XYConstraints(5, 7, 100, -1));
    jPanel1.add(jButton_ChangText,   new XYConstraints(6, 37, 90, 22));
    jPanel2.add(jComboBox_TopLeft, new XYConstraints(7, 9, 67, -1));
    jPanel2.add(jComboBox_TopRight,  new XYConstraints(85, 9, 67, -1));
    jPanel2.add(jComboBox_ButtomLeft,    new XYConstraints(167, 9, 67, -1));
    jPanel2.add(jComboBox_ButtomRight,   new XYConstraints(249, 9, 67, -1));
    jPanel2.add(jLabel1,  new XYConstraints(18, 42, 44, -1));
    jPanel2.add(jLabel2, new XYConstraints(100, 42, 40, -1));
    jPanel2.add(jLabel3, new XYConstraints(184, 41, 42, -1));
    jPanel2.add(jLabel4, new XYConstraints(262, 42, 44, -1));
    this.add(jPanel4,  new XYConstraints(478, 419, 116, 68));
    jPanel4.add(jSlider2,     new XYConstraints(4, 10, 104, -1));
    jPanel4.add(jLabel6,    new XYConstraints(16, 39, -1, -1));
    this.add(jPanel2, new XYConstraints(140, 418, 332, 71));
    //////////////////////////////////////////////////////////////
    // 自己初始化工作
    jComboBox_TopLeft.addItem(strGeometryType_Cube);
    jComboBox_TopLeft.addItem(strGeometryType_Cone);
    jComboBox_TopLeft.addItem(strGeometryType_Prim);
    jComboBox_TopLeft.addItem(strGeometryType_withTex);

    jComboBox_TopRight.addItem(strGeometryType_Cube);
    jComboBox_TopRight.addItem(strGeometryType_Cone);
    jComboBox_TopRight.addItem(strGeometryType_Prim);
    jComboBox_TopRight.addItem(strGeometryType_withTex);
    jComboBox_TopRight.setSelectedIndex(1);

    jComboBox_ButtomLeft.addItem(strGeometryType_Cube);
    jComboBox_ButtomLeft.addItem(strGeometryType_Cone);
    jComboBox_ButtomLeft.addItem(strGeometryType_Prim);
    jComboBox_ButtomLeft.addItem(strGeometryType_withTex);
    jComboBox_ButtomLeft.setSelectedIndex(2);

    jComboBox_ButtomRight.addItem(strGeometryType_Cube);
    jComboBox_ButtomRight.addItem(strGeometryType_Cone);
    jComboBox_ButtomRight.addItem(strGeometryType_Prim);
    jComboBox_ButtomRight.addItem(strGeometryType_withTex);
    jComboBox_ButtomRight.setSelectedIndex(3);

    GraphicsConfiguration config =
        SimpleUniverse.getPreferredConfiguration();
    // 创建3D画布
    Canvas3D c = new Canvas3D(config);
    add(c,  new XYConstraints(10, 10, 580, 390));

    // 设置视角
    ViewingPlatform viewingPlatform;
    Viewer viewer = new Viewer(c);
    Vector3d viewpoint = new Vector3d(0.0, 0.0, 20.0);//初始观察点位置
    Transform3D t = new Transform3D();
    t.set(viewpoint);
    ViewingPlatform v = new ViewingPlatform();
    v.getViewPlatformTransform().setTransform(t);

    // 创建图元和虚拟世界
    scence = createSceneGraph();
    u = new SimpleUniverse(v, viewer);
    scence.setCapability(scence.ALLOW_CHILDREN_WRITE );
    scence.setCapability(scence.ALLOW_CHILDREN_EXTEND);
    scence.setCapability(scence.ALLOW_CHILDREN_READ);


    bgTopLeft=createObject(2.0,new Vector3d(-7.0,4.5,0.0),4000,"立方体");
    bgTopRight =createObject(2.0,new Vector3d(7.0,4.5,0.0),4000,"圆锥");
    bgButtomLeft= createObject(2.0,new Vector3d(-7.0,-4.5,0.0),4000,"棱锥");
    bgButtomRight = createObject(2.0,new Vector3d(7.0,-4.5,0.0),4000,"带纹理形体");
    bgCenterText =createText(2.0,4000);

    scence.addChild(bgTopLeft);
    scence.addChild(bgTopRight);
    scence.addChild(bgButtomLeft);
    scence.addChild(bgButtomRight);
    scence.addChild(bgCenterText);

    u.addBranchGraph(scence);
    /////////////////////////////////////////////////////////////////////////
  }
  //Get Applet information
  public String getAppletInfo() {
    return "Applet Information";
  }
  //Get parameter info
  public String[][] getParameterInfo() {
    String[][] pinfo =
      {
      {"Text", "String", ""},
      };
    return pinfo;
  }
  // 根新场景图
  public void updataView(){
    if(scence== null)
      return ;
    scence.removeChild(bgTopLeft);
    scence.removeChild(bgTopRight);
    scence.removeChild(bgButtomLeft);
    scence.removeChild(bgButtomRight);
    scence.removeChild(bgCenterText);
    String strType[]= new String [4];
    for(int i=0; i<4;i++)
    {
      switch (ibgType[i])
      {
        case 1:
          strType[i] = strGeometryType_Cube;
          break;
        case 2:
          strType[i] = strGeometryType_Cone;
          break;
        case 3:
          strType[i] = strGeometryType_Prim;
          break;
        case 4:
          strType[i] = strGeometryType_withTex;
          break;
      }
    }
    bgTopLeft=createObject(2.0,new Vector3d(-7.0,4.5,0.0),fD,strType[0]);
    bgTopRight =createObject(2.0,new Vector3d(7.0,4.5,0.0),fD,strType[1]);
    bgButtomLeft= createObject(2.0,new Vector3d(-7.0,-4.5,0.0),fD,strType[2]);
    bgButtomRight = createObject(2.0,new Vector3d(7.0,-4.5,0.0),fD,strType[3]);
    bgCenterText =createText(2.0,fD);

    scence.addChild(bgTopLeft);
    scence.addChild(bgTopRight);
    scence.addChild(bgButtomLeft);
    scence.addChild(bgButtomRight);
    scence.addChild(bgCenterText);
  }
  // 创建图元
  public BranchGroup createSceneGraph() {
    float sl = txtString.length();
    // 创建根图元
    BranchGroup objRoot=new BranchGroup();

    // 创建一个变化对象,初始化为单位矩阵,并设置为可写
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    // 创建一个球形区域
    BoundingSphere bounds =
        new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    //设置背景颜色
    String bgImage="E:\\Java\\JProject\\J3DDemo\\classes\\j3ddemo\\bg.jpg";
    Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
    TextureLoader bgTexture = new TextureLoader(bgImage, this);
    Background bgNode = new Background(bgTexture.getImage());
    bgNode.setApplicationBounds(bounds);
    objRoot.addChild(bgNode);

    // 设置漫反射光源
    Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
    AmbientLight ambientLightNode = new AmbientLight(ambientColor);
    ambientLightNode.setInfluencingBounds(bounds);
    objRoot.addChild(ambientLightNode);

    // 设置线光源
    Color3f light1Color = new Color3f(0.0f, 0.8f, 0.0f);
    Vector3f light1Direction = new Vector3f(1.0f, 0.0f, 0.0f);
    Color3f light2Color = new Color3f(0.8f, 0.0f, 0.0f);
    Vector3f light2Direction = new Vector3f( -1.0f, 0.0f, 0.0f);

    DirectionalLight light1
        = new DirectionalLight(light1Color, light1Direction);
    light1.setInfluencingBounds(bounds);
    objRoot.addChild(light1);

    DirectionalLight light2
        = new DirectionalLight(light2Color, light2Direction);
    light2.setInfluencingBounds(bounds);
    objRoot.addChild(light2);

   return  objRoot;
  }

  static double cPi=3.1415926535897932384626433832795;
  // 按照指定的类型创建一个形体,参数分别是平移距离,位置矢量,旋转角速度和类型
  public Group createObject(double scale, Vector3d vPos,
                            int fD, String strType) {
    BranchGroup bg=new BranchGroup();
    // 创建一个变换对象节点.
    Transform3D t = new Transform3D();
    t.set(scale, vPos);
    TransformGroup objTrans = new TransformGroup(t);

    // 创建第二个变换节点。

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -