Java3D 3DS FileLoader包下载:http://sourceforge.net/projects/java3dsloader import com.microcrowd.loader.java3d.max3ds.Loader3DS; import com.sun.j3d.loaders.Scene; import com.sun.j3d.utils.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.behaviors.mouse.MouseTranslate; import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom; import com.sun.j3d.utils.universe.SimpleUniverse; import java.awt.Color; import java.io.FileNotFoundException; import java.util.logging.Level; import java.util.logging.Logger; import javax.media.j3d.Alpha; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.DirectionalLight; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.vecmath.Color3f; import javax.vecmath.Point3d; import javax.vecmath.Vector3f; /** * * @author hadeslee */ public class Simple3DSLd { public Simple3DSLd() { build(); } private void build() { try { BranchGroup bg = loadBranchGroup(); SimpleUniverse u = new SimpleUniverse(); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(bg); } catch (FileNotFoundException ex) { Logger.getLogger(Simple3DSLd.class.getName()).log(Level.SEVERE, null, ex); } } private BranchGroup loadBranchGroup() throws FileNotFoundException { Loader3DS l3ds = new Loader3DS(); Scene scene = l3ds.load("K:/sample.3ds"); System.out.println("scene=" + scene); if (scene != null) { BranchGroup objRoot = new BranchGroup(); BranchGroup bg = scene.getSceneGroup(); TransformGroup objTrans = new TransformGroup(); Transform3D trans = new Transform3D(); trans.setScale(0.3f); objTrans.setTransform(trans); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.addChild(bg); objRoot.addChild(objTrans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); MouseRotate behavior = new MouseRotate(); behavior.setTransformGroup(objTrans); objTrans.addChild(behavior); behavior.setSchedulingBounds(bounds); MouseTranslate tr = new MouseTranslate(); tr.setTransformGroup(objTrans); objTrans.addChild(tr); tr.setSchedulingBounds(bounds); MouseWheelZoom tr1 = new MouseWheelZoom(); tr1.setTransformGroup(objTrans); objTrans.addChild(tr1); tr1.setSchedulingBounds(bounds); Color3f light1Color = new Color3f(Color.RED); Vector3f light1Direction = new Vector3f(0f, 0f, -10f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } return null; } public static void main(String[] args) { new Simple3DSLd(); } } 或者Jframe中 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.DirectionalLight; import javax.media.j3d.GraphicsConfigTemplate3D; import javax.media.j3d.Locale; 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.microcrowd.loader.java3d.max3ds.Loader3DS; import com.sun.j3d.loaders.Scene; import com.sun.j3d.utils.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.behaviors.mouse.MouseTranslate; import com.sun.j3d.utils.behaviors.mouse.MouseWheelZoom; // import com.sun.j3d.utils.universe.SimpleUniverse; /** * @author * */ public class AWTFrameDSM { private static final long serialVersionUID = 1L; /** * 测试方法 */ public static void main(String[] args) { AWTFrameDSM appwin = new AWTFrameDSM(); appwin.initialize(); } /** * This is the default constructor */ public AWTFrameDSM() { } /** * 初始化Frame主窗口 * * @return void */ private void initialize() { // 实例化GraphicsConfiguration的方法1 GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D(); GraphicsEnvironment env = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice device = env.getDefaultScreenDevice(); GraphicsConfiguration config = device.getBestConfiguration(template); // 实例化GraphicsConfiguration的方法2 // GraphicsConfiguration config = // SimpleUniverse.getPreferredConfiguration(); // 新建Frame窗口对象 Frame dsframe = new Frame(config); // 若Frame对象的布局方式为绝对布局null,就无法显示3D虚拟场景 dsframe.setLayout(new BorderLayout()); dsframe.setTitle("3DS模型显示区"); dsframe.setBounds(new Rectangle(0, 0, 433, 458)); // 添加窗口监听器实现关闭窗口(Frame),关闭窗口时退出程序 dsframe.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.out.print("退出程序!"); System.exit(0); } }); // 新建3D场景绘制画布Canvas3D对象 Canvas3D cv = new Canvas3D(config); // add the canvas to this frame. Since this is the only thing added to // the main frame we don't care about layout managers etc. dsframe.add(cv, BorderLayout.CENTER); // 使Frame对象可视化 dsframe.setVisible(true); // 在Canvas3D画布上绘制3D场景 creat3DObject(cv); } /** * 创建3D虚拟场景 */ private void creat3DObject(Canvas3D cv) { // 创建超结构(create superstructure) VirtualUniverse vu = new VirtualUniverse(); Locale loc = new Locale(vu); // 创建视图分支(create view branch) BranchGroup bgView = createViewBranch(cv); bgView.compile(); loc.addBranchGraph(bgView); BranchGroup bg = loadBranchGroup(); bg.compile(); loc.addBranchGraph(bg); } /** * 生成BranchGroup的私有方法,创建视图分支 * * @param cv * Canvas3D画布对象 */ private BranchGroup createViewBranch(Canvas3D cv) { // 创建View组件对象 View view = new View(); // 设置投影方式 view.setProjectionPolicy(View.PERSPECTIVE_PROJECTION); // 创建ViewPlatform叶节点 ViewPlatform vp = new ViewPlatform(); view.addCanvas3D(cv); view.attachViewPlatform(vp); view.setPhysicalBody(new PhysicalBody()); // 设置View对象属性 view.setPhysicalEnvironment(new PhysicalEnvironment()); // 几何变换 Transform3D trans = new Transform3D(); // 观察者眼睛的位置 Point3d eye = new Point3d(0, 0, 1 / Math.tan(Math.PI / 8)); // 观察者方向指向的点 Point3d center = new Point3d(0, 0, 0); // 垂直于观察者方向向上的方向 Vector3d vup = new Vector3d(0, 1, 0); // 生成几何变换矩阵 trans.lookAt(eye, center, vup); // 求矩阵的逆 trans.invert(); // 几何变换组点 TransformGroup tg = new TransformGroup(trans); tg.addChild(vp); // 创建视图分支 BranchGroup bgView = new BranchGroup(); bgView.addChild(tg); return bgView; } /** * 加载3DS文件的场景内容分支 */ private BranchGroup loadBranchGroup() { Loader3DS l3ds = new Loader3DS(); // The Scene interface is a set of methods used to extract Java 3D scene // graph information from a file loader utility. // The interface is used to give loaders of various file formats a // common public interface. Scene scene = null; try { scene = l3ds.load("K:/opengheadok.3DS"); System.out.println("文件读取成功"); } catch (Exception fileio) { System.out.println("文件读取失败,请检查文件是否存在"); } System.out.println("scene=" + scene); if (scene != null) { BranchGroup bg = scene.getSceneGroup(); // 创建场景图分支 BranchGroup objRoot = new BranchGroup(); // 几何变换 Transform3D trans = new Transform3D(); trans.setScale(0.3f);// 缩放变换 // 几何变换组节点 TransformGroup objTrans = new TransformGroup(); objTrans.setTransform(trans); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.addChild(bg); objRoot.addChild(objTrans); // 球体作用范围边界对象 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // 添加通过鼠标左键控制3D物体旋转的对象 MouseRotate behavior = new MouseRotate(); behavior.setTransformGroup(objTrans); objTrans.addChild(behavior); behavior.setSchedulingBounds(bounds); // 添加鼠标右键的拖拉运动控制3D物体(X,Y)平移 MouseTranslate tr = new MouseTranslate(); tr.setTransformGroup(objTrans); objTrans.addChild(tr); tr.setSchedulingBounds(bounds); // 添加鼠标滚轮控制3D物体沿Z轴 MouseWheelZoom tr1 = new MouseWheelZoom(); tr1.setTransformGroup(objTrans); objTrans.addChild(tr1); tr1.setSchedulingBounds(bounds); // 设置光源 Color3f light1Color = new Color3f(Color.gray); Vector3f light1Direction = new Vector3f(0f, 0f, -10f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); return objRoot; } return null; } }