📄 bouncingball.java
字号:
package csu.chailei.test;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Sphere;
import javax.swing.Timer;
public class BouncingBall extends Applet implements ActionListener, KeyListener {
private Button go = new Button("Go");
private Button test=new Button("你好");
private TransformGroup objTrans;
private Transform3D trans = new Transform3D();
private float height = 0.0f;
private float sign = 1.0f; // going up or down
private Timer timer;
private float xloc = 0.0f;
public BranchGroup createSceneGraph() {
// 创建分支图的根
BranchGroup objRoot = new BranchGroup();
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
// 创建一个简单图形叶子节点,添加到场景图。
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),
100.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 BouncingBall() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
//add("North",new Label("你好"));
add("Center", c);
add("South",new TextField("天上人间"));
c.addKeyListener(this);
timer = new Timer(100, this);
// timer.start();
Panel p = new Panel();
p.add(go);
p.add(test);
add("North", p);
go.addActionListener(this);
go.addKeyListener(this);
// 创建一个简单场景附加到虚拟宇宙
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
}
public void keyPressed(KeyEvent e) {
// 按键被按下时调用
if (e.getKeyChar() == 's') {
xloc = xloc + .1f;
}
if (e.getKeyChar() == 'a') {
xloc = xloc - .1f;
}
}
public void keyReleased(KeyEvent e) {
// 按键放开是被调用
}
public void keyTyped(KeyEvent e) {
// 按一个键时调用
}
public void actionPerformed(ActionEvent e) {
// 按钮按下时启动计时器
if (e.getSource() == go) {
if (!timer.isRunning()) {
timer.start();
}
} else {
height += .1 * sign;
if (Math.abs(height * 2) >= 1)
sign = -1.0f * sign;
if (height < -0.4f) {
trans.setScale(new Vector3d(1.0, .8, 1.0));
} else {
trans.setScale(new Vector3d(1.0, 1.0, 1.0));
}
trans.setTranslation(new Vector3f(xloc, height, 0.0f));
objTrans.setTransform(trans);
}
}
public static void main(String[] args) {
System.out.println("Program Started");
BouncingBall bb = new BouncingBall();
bb.addKeyListener(bb);
MainFrame mf = new MainFrame(bb, 256, 256);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -