【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-BouncingBall.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package book.mutimedia;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GraphicsConfiguration;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
/**
* 三维弹球动画
*
* 想要制作动画你就要在动画的每一帧间移动物体。你可以使用计时器并且每次经过一个很短的时间移动物体。
* 你也可以用其它方式修改物体,下面的例子缩放了小球使它看起来在每次碰撞时被压扁。 为了和用户交互,你可以处理按键或按钮点击或其它组件。
* 有一点需要注意的是你必须通过设置功能属性告诉Java 3D你要移动物体。否则一旦物体被绘制你将不能再移动它们。
* 下面的例子组合了这些技术。点击按钮使它启动,小球开始上下跳动,然后你可以按下a或s左右移动小球
*/
public class BouncingBall extends JFrame implements ActionListener, KeyListener {
// 启动按钮
private Button go = new Button("开 始");
// 三维变换和变换组
private TransformGroup objTrans;
private Transform3D trans = new Transform3D();
// 球的Y坐标位置,为0表示在中心,为复数表示在下面,正数表示在上面
private float hPosition = 0.0f;
// 球的横坐标位置,位0表示处于正中心,为负数表示在左边,正数表示在右边
private float xPosition = 0.0f;
// 球上下运动的方向,1表示向上运动,-1表示向下运动
private float sign = 1.0f;
// 定时器,不断的将球上下移动
private Timer timer;
/**
* 构造函数
*/
public BouncingBall() {
this.getContentPane().setLayout(new BorderLayout());
// 获得宇宙的配置
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
// 根据宇宙配置创建一个Canvas3D,3D的画布
Canvas3D c = new Canvas3D(config);
this.getContentPane().add("Center", c);
c.addKeyListener(this);
// 创建一个定时器
timer = new Timer(100, this);
Panel p = new Panel();
p.add(go);
this.getContentPane().add("North", p);
go.addActionListener(this);
go.addKeyListener(this);
// 创建一个简单场景附加到虚拟宇宙
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
/**
* 创建三维球
*/
public BranchGroup createSceneGraph() {
// 创建容纳球的结构
BranchGroup objRoot = new BranchGroup();
// 创建三维球,并将球放置到原点
Sphere sphere = new Sphere(0.25f);
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
objTrans.setTransform(pos1);
objTrans.addChild(sphere);
objRoot.addChild(objTrans);
// 创建定向光源
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
1000.0);
Color3f light1Color = new Color3f(1.0f, 0.0f, 0.2f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
// 设置环境光
Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
return objRoot;
}
/**
* 处理键盘时间
*/
public void keyPressed(KeyEvent e) {
// 按键被按下时调用
if (e.getKeyChar() == 's') {
xPosition = xPosition + 0.1f;
}
if (e.getKeyChar() == 'a') {
xPosition = xPosition - 0.1f;
}
}
public void keyTyped(KeyEvent e) {
// do nothing
}
public void keyReleased(KeyEvent e) {
// do nothing
}
/**
* 时间处理,包括按钮事件和定时器的事件
*/
pub