Code:
import java.applet.Applet;
java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event .*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe .*;
import com.sun.j3d.utils.geometry .*;
javax.media.j3d import .*;
javax.vecmath import .*;
import com.sun.j3d.utils.image.TextureLoader;
SphereTexture class extends Applet
{Public SphereTexture (String [] args) {
setLayout (new BorderLayout ());
Canvas3D Canvas3D = new Canvas3D (null) / / derived from the AWT Canvas class
add ("Center", Canvas3D);
BranchGroup scene = scene graph create (args);
scene.compile ();
/ / Utility class used SimpleUniverse
SimpleUniverse simpleU = new SimpleUniverse (Canvas3D);
/ / Moves the view platform by default
simpleU.getViewingPlatform () setNominalViewingTransform ();.
simpleU.addBranchGraph (scene) / / live set
} / / End of constructor
public BranchGroup scene graph create (String [] args) {
/ / Creates the root of the branch graph
BranchGroup objRoot = new BranchGroup ();
/ / Build wTransformGroup for the rotation of the ball
tgSpin TransformGroup = new TransformGroup ();
tgSpin.setCapability (TransformGroup.ALLOW_TRANSFORM_WRITE);
/ / Adds to tgSpin tgRotate to
objRoot.addChild (tgSpin);
/ / Create Appearance object
Appearance appearance = new Appearance ();
if (args.length> 0) {
/ / Use TextureLoader utility class
Texture tex = new TextureLoader (args [0], this) GetTexture ();.
appearance.setTexture (tex);
if (tex == null)
System.out.println ("Warning: texture is not activated!");
}
else {
System.out.println ("Warning: do not pass any texture file.");
System.out.println ("Usage: java TextureImage" image filename "[-f image component format]");
}
/ Creates a Sphere Leaf nodes, and adds to it
/ / The newly created TransformGroup tgSpin added.
/ / Utility class used Sphere with texture
Sphere sphere = new Sphere (0.6f,
Sphere.GENERATE_TEXTURE_COORDS appearance);
tgSpin.addChild (sphere);
/ / Create alpha object
Alpha alpha = new Alpha (-1.24000);
/ / Create object and Behaviour reference to tgSpin TransformGroup
RotationInterpolator rotator = new RotationInterpolator (alpha, tgSpin);
/ / Will BoundingSphere object that defines the boundaries for behavior
/ / (Default)
BoundingSphere BoundingSphere bounds = new ();
rotator.setSchedulingBounds (bounds);
/ / Add object to Behaviour tgSpin to TransformGroup
tgSpin.addChild (rotator);
objRoot return;
} / / End of scene graph create () method
public static void main (String [] args) {
Frame frame = new MainFrame (new SphereTexture (args), 256, 256);
} / / End main method
} / / End of class SphereTexture
Bookmarks