/* * BallJ3D.java * Created on Feb 11, 2005 */ package ball; import java.applet.Applet; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GraphicsConfiguration; import javax.media.j3d.Appearance; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.ColoringAttributes; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.vecmath.Color3f; import javax.vecmath.Point3d; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.geometry.Sphere; import com.sun.j3d.utils.universe.SimpleUniverse; /** * @author gdennis * * A bouncing ball applet using Java 3D OpenGL */ public final class BallJ3D extends Applet { private static final float RADIUS = 0.15f; private static final double SPEED = 0.05; private static final Color COLOR1 = Color.BLUE; private static final Color COLOR2 = Color.RED; private SimpleUniverse universe = null; private Transform3D location = null; public BranchGroup createSceneGraph() { // create the root of the branch graph BranchGroup root = new BranchGroup(); // create the ball that we will bounce Color3f color = new Color3f(COLOR1); ColoringAttributes colorAttribs = new ColoringAttributes(color, ColoringAttributes.SHADE_GOURAUD); Appearance appear = new Appearance(); appear.setColoringAttributes(colorAttribs); Sphere ball = new Sphere(RADIUS, appear); // create a transform group with the ball TransformGroup trans = new TransformGroup(); trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); root.addChild(trans); trans.addChild(ball); // add a bounce interpolator to the scene graph BounceInterpolator bounce = new BounceInterpolator(RADIUS, SPEED, ball.getAppearance().getColoringAttributes(), COLOR1, COLOR2, trans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);; bounce.setSchedulingBounds(bounds); root.addChild(bounce); // optimization scene graph and return it root.compile(); return root; } public void init() { // create 3d canvas GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(config); // create universe BranchGroup scene = createSceneGraph(); universe = new SimpleUniverse(canvas); // move ViewPlatform back a bit so objects can be viewed universe.getViewingPlatform().setNominalViewingTransform(); universe.addBranchGraph(scene); // add canvas to layout setLayout(new BorderLayout()); add(canvas, BorderLayout.CENTER); } public void destroy() { universe.cleanup(); } public static void main(String[] args) { new MainFrame(new BallJ3D(), 500, 500); } }