原文地址:搭建JAVA3D环境配置作者:项伯羽

1、下载安装 JDK程序:jdk-1_5_0_07-windows-i586-p.exe

2、设置环境变量:方法为“我的电脑”属性---高级---环境变量

   增加“CLASSPATH”内容为“.;I:Program FilesJavajdk1.5.0_07libtools.jar;I:Program FilesJavajdk1.5.0_07libdt.jar;d:class;”。注意安装位置路径!

   增加“JAVA_HOME”内容为“I:Program FilesJavajdk1.5.0_07;”

   在PATH中添加“I:Program FilesJavajdk1.5.0_07bin;I:Program FilesJavajdk1.5.0_07jre/bin;”在前面加的。

3、进入CMD,在任意盘符下试运行JACAC看是否正常。要多试几次的。可进入JAVAC所在目录运行先。

4、如果路径没问题,可以在记事本上输入

   public class HelloJAVA
{
public static void main(String[] args)
{
System.out.println("Hello JAVA!");
}
}

更名为HelloJAVA.java,放在D:/CLASS

5、在CMD后运行

   D:CLASSjavac hellojava.java    (称作编译)

   成功后

   D:CLASjava  HelloJAVA   (注意大小写!就是这里我费了一些脑筋,就会报:Exception in Thread main java.lang.NoClassDefFoundError

   应该出现  Hello JAVA!!字样,至此,JAVA JDK环境基本完工。

   以上是查了网上资料,但是也有相当初学者遇到麻烦,不知本人的操作有什么问题吗。有一点,如果不是亲身实践那么总以为容易的。

6、下载并安装程序:java3d-1_5_1-windows-i586.exe

    这个程序与前面的JDK安装先后不能搞错。这时安装可见到本程序自动发现JDK的安装路径。默认在C:,无法更改的样子。

7、在D:/CLASS下建立文本文件,内容为:

 

import java.applet.Applet;
import java.awt.BorderLayout;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;

public class UglyCube extends Applet 
{
private SimpleUniverse universe ;

public UglyCube() 
{}    

public void init() 
{      
     //canvas to draw on, ask SimpleUniverse what config to use     
     Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());      
     setLayout(new BorderLayout());      
     add("Center", canvas);           

     //create top of our scene graph      
     BranchGroup scene = new BranchGroup();               

     //create universe, and attach our geometry to it.      
     SimpleUniverse u = new SimpleUniverse(canvas);      
     u.getViewingPlatform().setNominalViewingTransform();              

     // Create the bounding leaf node
     BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

     // Create the transform node
     TransformGroup transformGroup = new TransformGroup();
     transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
     transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

     // Create the drag behavior node
     MouseRotate behavior = new MouseRotate();
     behavior.setTransformGroup(transformGroup);
     transformGroup.addChild(behavior);
     behavior.setSchedulingBounds(bounds);

     // Create the zoom behavior node
     MouseZoom behavior2 = new MouseZoom();
     behavior2.setTransformGroup(transformGroup);
     transformGroup.addChild(behavior2);
     behavior2.setSchedulingBounds(bounds);

     // Create the zoom behavior node
     MouseTranslate behavior3 = new MouseTranslate();
     behavior3.setTransformGroup(transformGroup);
     transformGroup.addChild(behavior3);
     behavior3.setSchedulingBounds(bounds);

     transformGroup.addChild(new ColorCube(0.4));
  
     scene.addChild(transformGroup);    
     u.addBranchGraph(scene);
}    
  
  
// The following allows UglyCube to be run as an application    
// as well as an applet    
public static void main(String[] args) 
{        
     new MainFrame(new UglyCube(), 256, 256);   
}
}

   然后把它把保存更名为UglyCube.java,然后用进入CMD

   D:CLASSJAVAC UglyCube.java    (编译)

   D:CLASSJAVA UglyCube          (运行)

   运行结果:这是个彩色六面体,可以用鼠标左键旋转,右键拖动,摁下滚轮前后移动缩放。

   测试Java3D开发运行环境是否配置正确工作至此完成。

============================ 

『『

附:下面这段程序只会出现一个旋转的立方体,鼠标没法控制

import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;


public class HelloUniverse extends javax.swing.JFrame {

    private SimpleUniverse univ = null;
    private BranchGroup scene = null;

    public BranchGroup createSceneGraph() {
 // Create the root of the branch graph
 BranchGroup objRoot = new BranchGroup();

 // Create the TransformGroup node and initialize it to the
 // identity. Enable the TRANSFORM_WRITE capability so that
 // our behavior code can modify it at run time. Add it to
 // the root of the subgraph.
 TransformGroup objTrans = new TransformGroup();
 objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
 objRoot.addChild(objTrans);

 // Create a simple Shape3D node; add it to the scene graph.
 objTrans.addChild(new ColorCube(0.4));

 // Create a new Behavior object that will perform the
 // desired operation on the specified transform and add
 // it into the scene graph.
 Transform3D yAxis = new Transform3D();
 Alpha rotationAlpha = new Alpha(-1, 4000);

 RotationInterpolator rotator =
     new RotationInterpolator(rotationAlpha, objTrans, yAxis,
         0.0f, (float) Math.PI*2.0f);
 BoundingSphere bounds =
     new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
 rotator.setSchedulingBounds(bounds);
 objRoot.addChild(rotator);

        // Have Java 3D perform optimizations on this scene graph.
        objRoot.compile();

 return objRoot;
    }

    private Canvas3D createUniverse() {
 // Get the preferred graphics configuration for the default screen
 GraphicsConfiguration config =
     SimpleUniverse.getPreferredConfiguration();

 // Create a Canvas3D using the preferred configuration
 Canvas3D c = new Canvas3D(config);

 // Create simple universe with view branch
 univ = new SimpleUniverse(c);

 // This will move the ViewPlatform back a bit so the
 // objects in the scene can be viewed.
 univ.getViewingPlatform().setNominalViewingTransform();

 // Ensure at least 5 msec per frame (i.e., < 200Hz)
 univ.getViewer().getView().setMinimumFrameCycleTime(5);

 return c;
    }

   
    public HelloUniverse() {
 // Initialize the GUI components
 initComponents();

 // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
 Canvas3D c = createUniverse();
 drawingPanel.add(c, java.awt.BorderLayout.CENTER);

 // Create the content branch and add it to the universe
 scene = createSceneGraph();
 univ.addBranchGraph(scene);
    }

    // ----------------------------------------------------------------
    
   
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        drawingPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HelloUniverse");
        drawingPanel.setLayout(new java.awt.BorderLayout());

        drawingPanel.setPreferredSize(new java.awt.Dimension(250, 250));
        getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HelloUniverse().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel drawingPanel;
    // End of variables declaration//GEN-END:variables
    
}

                                                  文件取名  HelloUniverse.java   』』

=====================

http://bsnmnc.blog.hexun.com/50458385_d.html

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()