编者按:我们今天所要摘录的是给所有java玩家的,尤其是3d爱好者,我们知道你关心这。这两部分的第一部分是从《java游戏编程杀手》第十五章摘出,作者andrew davison描述了怎样在一个3d跳棋程序中用java建立一个场景。下周,andrew将说明怎样为这个3d跳棋程序建立一个浮动的球体。
版权声明:任何获得matrix授权的网站,转载时请务必保留以下作者信息和链接
作者:launze
原文:http://www.onjava.com/pub/a/onjava/excerpt/kgp_in_java_chap15/index.html
译文:http://www.matrix.org.cn/resource/article/44/44216_java+killer+game.html
关键字:java;killer;game
这章用一个java 3d例子来描述一个3d跳棋。这个例子建立了一个场景,包括:由一个暗绿色和兰色格相交平铺的,并带有标签的x轴和z轴形成的平面;一个兰色背景;一个可以在两个不同方向浮动的球体。用户可以通过鼠标来浏览(拉近放远)场景。
左边的截图15-1显示最初视图,右边的图是用户视图移动一些之后的效果。
figure 15-1. initial view, and later
3d跳棋阐述了java 3d编程中一些常用的方法和一些小窍门。例如,3d场景使用canvas3d类来实现显示(这个类和swing组件结合使用)。所有的java 3d程序需要一个场景图,3d跳棋说明了如何增加基本图形,灯光,背景。这些场景图形成了文件的可视形式,记录这些场景信息的文本版本通过daniel selman的java3dtree包很容易就能实现。(在这节的最后我会详细介绍)
地板和球体使用了java3d的quadarray, text2d, and sphere几何类:地板是由quadarray的一系列四边形组成,标签是用text2d对象沿着地板上的主轴形成。用户通过一个观察点查看这个3d世界,你将看到如何初始放置观察点、在使用java3d的orbitbehavior 类时候如何移动观察点。
3d跳棋类图
图15-2的类图说明了3d跳棋程序的public和private数据项和方法。
figure 15-2. class diagrams for checkers3d
checkers3d 是程序的顶层jframe . wrapcheckers3d 是场景图拥有的 jpanel ,作为一个canvas3d 对象,他是可视的. checkerfloor 建立地板的子图(例如方格,轴), with所有同颜色的方格用单独的coloredtiles 对象表示。
提示:例子的原代码在checkers3d/目录(可能是原书附带光盘)
java 3d和swing 的结合
就向swing文本和按纽,checkers3d 是gui控制位置的的一个 jframe ,把他放在必要的地方。在这个例子中,他建立一个wrapcheckers3d (一个 jpanel) 实例,并把他放在 borderlayout 中间。
c.setlayout( new borderlayout( ) );
wrapcheckers3d w3d = new wrapcheckers3d( ); // panel for 3d canvas
c.add(w3d, borderlayout.center);
public wrapcheckers3d( )
{
setlayout( new borderlayout( ) );
// other initialization code
graphicsconfiguration config =
simpleuniverse.getpreferredconfiguration( );
canvas3d canvas3d = new canvas3d(config);
add("center", canvas3d);
// other initialization code}
while(true) {
process user input; //用户输入
if (exit request) break;
perform behaviors;
if (scene graph has changed) //场景改变
traverse scene graph and render; //移动场景视图
}public wrapcheckers3d( )
{
// initialization code
graphicsconfiguration config =
simpleuniverse.getpreferredconfiguration( );
canvas3d canvas3d = new canvas3d(config);
add("center", canvas3d);
canvas3d.setfocusable(true); // give focus to the canvas
canvas3d.requestfocus( );
su = new simpleuniverse(canvas3d);
createscenegraph( );
inituserposition( ); // set user's viewpoint
orbitcontrols(canvas3d); // controls for moving the viewpoint
su.addbranchgraph( scenebg );
}
private void createscenegraph( )
{
scenebg = new branchgroup( );
bounds = new boundingsphere(new point3d(0,0,0), boundsize);
lightscene( ); // add the lights
addbackground( ); // add the sky
scenebg.addchild( new checkerfloor( ).getbg( ) ); // add floor
floatingsphere( ); // add the floating sphere
scenebg.compile( ); // fix the scene
} // end of createscenegraph( )
color3f white = new color3f(1.0f, 1.0f, 1.0f);
// set up the ambient light
ambientlight ambientlightnode = new ambientlight(white);
ambientlightnode.setinfluencingbounds(bounds);
scenebg.addchild(ambientlightnode);
vector3f light1direction = new vector3f(-1.0f, -1.0f, -1.0f);
// left, down, backwards
directionallight light1 = new directionallight(white, light1direction);
light1.setinfluencingbounds(bounds);
scenebg.addchild(light1);
material mat = new material(ambientcolor, emissivecolor,
diffusecolor, specularcolor, shininess);
color3f black = new color3f(0.0f, 0.0f, 0.0f);
color3f blue = new color3f(0.3f, 0.3f, 0.8f);
color3f specular = new color3f(0.9f, 0.9f, 0.9f); // near white
material bluemat= new material(blue, black, blue, specular, 25.0f);
bluemat.setlightingenable(true);
appearance blueapp = new appearance( );
blueapp.setmaterial(bluemat);
transform3d t3d = new transform3d( );
t3d.set( new vector3f(0,4,0)); // place at (0,4,0)
transformgroup tg = new transformgroup(t3d);
tg.addchild(new sphere(2.0f, blueapp));
// set the sphere's radius and appearance
// and its normals by default
scenebg.addchild(tg);
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器