类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:










页面导航:
正文内容:2005.04.26用java3d写了一个金字塔。
1.打开jcreator3.5,进入漂亮界面。点击file->new->file弹出文件保存对话框,写好你要保存的文件名和文件保存路径。引入我们要调用的包:
import java.applet.applet;
import java.awt.borderlayout;
import java.awt.frame;
import java.awt.graphicsconfiguration;
import com.sun.j3d.utils.applet.mainframe;
import com.sun.j3d.utils.geometry.colorcube;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.behaviors.mouse.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.util.enumeration;
2.现在我们正式开动了。先写一个我们要展现的金字塔。
public class test_pyramid extends applet
{
shape3d createpyramid() //因为我们要创建的金字塔是shape3d类型。
{
indexedtrianglearray pygeom =
new indexedtrianglearray(5, geometryarray.coordinates
| geometryarray.color_3
, 18);
// 这是用数组来创建一连串三角形的函数。其中5是我们创建模型所需的顶点数
// geometryarray.coordinates 是用坐标点来构建面。
// 18 是来构建这个金字塔所用到的索引点数。
// 定义5个顶点
pygeom.setcoordinate(0,new point3f( 0.0f, 0.7f, 0.0f));
pygeom.setcoordinate(1,new point3f( -0.4f, 0.0f, -0.4f));
pygeom.setcoordinate(2,new point3f( -0.4f, 0.0f, 0.4f));
pygeom.setcoordinate(3,new point3f( 0.4f, 0.0f, 0.4f));
pygeom.setcoordinate(4,new point3f( 0.4f, 0.0f, -0.4f));
// 构建第一个三角形面。
pygeom.setcoordinateindex(0,0);
pygeom.setcoordinateindex(1,1);
pygeom.setcoordinateindex(2,2);
pygeom.setcoordinateindex(3,0);
pygeom.setcoordinateindex(4,2);
pygeom.setcoordinateindex(5,3);
pygeom.setcoordinateindex(6,0);
pygeom.setcoordinateindex(7,3);
pygeom.setcoordinateindex(8,4);
pygeom.setcoordinateindex(9,0);
pygeom.setcoordinateindex(10,4);
pygeom.setcoordinateindex(11,1);
pygeom.setcoordinateindex(12,1);
pygeom.setcoordinateindex(13,4);
pygeom.setcoordinateindex(14,2);
pygeom.setcoordinateindex(15,4);
pygeom.setcoordinateindex(16,3);
pygeom.setcoordinateindex(17,2);
color3f c = new color3f(0.6f,0.5f,0.55f);
pygeom.setcolor(0,c);
pygeom.setcolor(1,c);
pygeom.setcolor(2,c);
pygeom.setcolor(3,c);
pygeom.setcolor(4,c);
shape3d pyramid = new shape3d(pygeom);
return pyramid;
}
3.创建视景根节点
public branchgroup createscenegraph()
{
//创建视景根节点
branchgroup objroot = new branchgroup();
transformgroup objrotate = new transformgroup();
objrotate.setcapability(transformgroup.allow_transform_write);
objrotate.setcapability(transformgroup.allow_transform_read);
objroot.addchild(objrotate);
objrotate.addchild(createpyramid());
mouserotate mymouserotate = new mouserotate();
mymouserotate.settransformgroup(objrotate);
mymouserotate.setschedulingbounds(new boundingsphere());
objroot.addchild(mymouserotate);
//让java3d预先优化
objroot.compile();
return objroot;
}
4.在初始化函数中配置applet显示
public test_pyramid()
{
setlayout(new borderlayout());
graphicsconfiguration config = simpleuniverse.getpreferredconfiguration();
canvas3d canvas3d = new canvas3d(config);
add("center",canvas3d);
branchgroup scene = createscenegraph();
//simpleuniverse是一个方便的工具类
simpleuniverse simpleu = new simpleuniverse(canvas3d);
simpleu.getviewingplatform().setnominalviewingtransform();
simpleu.addbranchgraph(scene);
}
5.最后。
public static void main(string[] args)
{
frame frame = new mainframe(new test_pyramid(),256,256);
}
}
如果编译成功的话,就可以显示了!有什么问题可以留言!