public class BouncingBall extends Applet implements ActionListener,
KeyListener {
private Button go = new Button("Go");
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() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
objTrans = new TransformGroup();
[Link](TransformGroup.ALLOW_TRANSFORM_WRITE);
[Link](objTrans);
// Create a simple shape leaf node, add it to the scene graph.
Sphere sphere = new Sphere(0.25f);
objTrans = new TransformGroup();
[Link](TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
[Link](new Vector3f(0.0f, 0.0f, 0.0f));
[Link](pos1);
[Link](sphere);
[Link](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);
[Link](bounds);
[Link](light1);
// Set up the ambient light
Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
[Link](bounds);
[Link](ambientLightNode);
return objRoot;
}
public BouncingBall() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
[Link](this);
timer = new Timer(100, this);
//[Link]();
Panel p = new Panel();
[Link](go);
add("North", p);
[Link](this);
[Link](this);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
SimpleUniverse u = new SimpleUniverse(c);
[Link]().setNominalViewingTransform();
[Link](scene);
public void keyPressed(KeyEvent e) {
//Invoked when a key has been pressed.
if ([Link]() == 's') {
xloc = xloc + .1f;
}
if ([Link]() == 'a') {
xloc = xloc - .1f;
}
}
public void keyReleased(KeyEvent e) {
// Invoked when a key has been released.
}
public void keyTyped(KeyEvent e) {
//Invoked when a key has been typed.
}
public void actionPerformed(ActionEvent e) {
// start timer when button is pressed
if ([Link]() == go) {
if (![Link]()) {
[Link]();
}
} else {
height += .1 * sign;
if ([Link](height * 2) >= 1)
sign = -1.0f * sign;
if (height < -0.4f) {
[Link](new Vector3d(1.0, .8, 1.0));
} else {
[Link](new Vector3d(1.0, 1.0, 1.0));
}
[Link](new Vector3f(xloc, height, 0.0f));
[Link](trans);
}
}
public static void main(String[] args) {
[Link]("Program Started");
BouncingBall bb = new BouncingBall();
[Link](bb);
MainFrame mf = new MainFrame(bb, 256, 256);
}