?? j3dapplet.java~141~
字號:
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 + -