Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.applet.Applet;
- import java.awt.event.*;
- import com.sun.j3d.utils.applet.MainFrame;
- import com.sun.j3d.utils.geometry.*;
- import com.sun.j3d.utils.universe.*;
- import javax.media.j3d.*;
- import javax.vecmath.*;
- import java.io.*;
- import java.awt.*;
- import com.sun.j3d.utils.behaviors.mouse.*;
- import com.sun.j3d.utils.behaviors.keyboard.*;
- import javax.swing.JApplet;
- import com.sun.j3d.utils.image.TextureLoader;
- import java.net.*;
- /**
- * Describe class Example3D here.
- *
- *
- * Created: Sun Jan 11 00:50:05 2010
- *
- * @author <a href="mailto:">M. Hoffmann</a>
- * @version 1.0
- */
- public class Example3D extends JApplet {
- static public final Color3f green = new Color3f(0.0f,1.0f,0.0f);
- static public final Color3f red = new Color3f(1.0f,0.0f,0.0f);
- static public final Color3f white = new Color3f(1.0f,1.0f,1.0f);
- static public final Color3f gray = new Color3f(.5f,.50f,.50f);
- static public final Color3f black = new Color3f(0.0f,0.0f,0.0f);
- static public final Color3f blue = new Color3f(0.0f,0.0f,1.0f);
- //
- static public final Rectangle recto = new Rectangle();
- //Pocket
- static public final Sphere ballz = new Sphere(0.5f);
- static public final float PATH_SIDE =15f;
- static public final float PATH_SIDE1 =30f;
- static public final float PILLAR_WIDTH =1.5f;
- static public final float PILLAR_HEIGHT=0.5f;
- //
- static public final float PILLAR_WIDTH1 =8f;
- static public final float PILLAR_HEIGHT1=9f;
- static public final float BALL_RADIUS =1f;
- static public final float HIT_RADIUS =1f;
- static public final float HIT_LENGTH =8f;
- //Size Of Pillars
- static public final float HEIGHT_DIFFERENCE = 0.5f;
- //THICKNESS OF SIDES
- static public final float WOOD_THICK = .4f;
- //SPEED OF BALL
- static public final float[] speed = {0.0f,.25f,.5f,.75f,1f};
- static public final float[] speed2 = {0.0f,.25f,.5f,.75f,1f};
- //PATHS
- private Point3f[] path ;
- private Point3f[] path2 ;
- //TEXTURE URLS
- private static URL stoneURL, stoneURL1, woodURL, sideLEFT, woodURL2, woodURL3 ,tableURL;
- public TransformGroup createPicture() {
- //POINTS FOR THE BALL
- path=new Point3f[5];
- path[0]=new Point3f(0f,0f,BALL_RADIUS);
- path[1]=new Point3f(PATH_SIDE,0f,BALL_RADIUS+HEIGHT_DIFFERENCE);
- path[2]=new Point3f(PATH_SIDE,PATH_SIDE,BALL_RADIUS+HEIGHT_DIFFERENCE);
- path[3]=new Point3f(0f,PATH_SIDE,BALL_RADIUS);
- path[4]=new Point3f(0f,0f,BALL_RADIUS);
- // THE POINTS
- path2=new Point3f[5];
- // THE POINT OF THE MOVE PILLAR
- path2[0]=new Point3f(4,PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
- //
- path2[1]=new Point3f(9,PATH_SIDE-PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
- path2[2]=new Point3f(PATH_SIDE,PATH_SIDE-PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
- path2[3]=new Point3f(PATH_SIDE,PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
- path2[4]=new Point3f(PATH_SIDE,PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
- Texture woodTex=null,stoneTex=null, stoneTex1=null, woodTex1=null, woodTex2=null, woodTex3=null,tableTex = null;
- // Load Textures
- stoneTex = new TextureLoader(stoneURL, this).getTexture();
- stoneTex1 = new TextureLoader(stoneURL1, this).getTexture();
- woodTex = new TextureLoader(woodURL, this).getTexture();
- woodTex1 = new TextureLoader(sideLEFT, this).getTexture();
- woodTex2 = new TextureLoader(woodURL2, this).getTexture();
- woodTex3 = new TextureLoader(woodURL3, this).getTexture();
- tableTex = new TextureLoader(tableURL,this).getTexture();
- // creating the universe
- BoundingSphere bounds =
- new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);
- TransformGroup mainTG = new TransformGroup();
- AmbientLight aLgt = new AmbientLight(gray);
- aLgt.setInfluencingBounds(bounds);
- DirectionalLight lgt1 = new DirectionalLight(gray, new Vector3f(-1.0f, -1.0f, -1.0f));
- lgt1.setInfluencingBounds(bounds);
- DirectionalLight ballLgt = new DirectionalLight(red, new Vector3f(-1.0f, -1.0f, -1.0f));
- ballLgt.setInfluencingBounds(bounds);
- // objRoot.addChild(aLgt);
- mainTG.addChild(lgt1);
- Appearance stoneApp = new Appearance();
- Appearance stoneApp1 = new Appearance();
- stoneApp.setTexture(stoneTex);
- stoneApp1.setTexture(stoneTex1);
- TextureAttributes texAttr = new TextureAttributes();
- texAttr.setTextureMode(TextureAttributes.MODULATE);
- stoneApp.setTextureAttributes(texAttr);
- stoneApp.setMaterial(new Material(white, black, white, black, 100.0f));
- Material ballMat=new Material(red, black, red, white, 100.0f);
- ballMat.setLightingEnable(true);
- // stoneApp.setMaterial(ballMat);
- //WOODEN BLOCK
- Appearance woodApp = new Appearance();
- Appearance sideApp = new Appearance();
- Appearance woodApp2 = new Appearance();
- Appearance woodApp3 = new Appearance();
- woodApp.setTexture(woodTex);
- sideApp.setTexture(woodTex1);
- woodApp2.setTexture(woodTex2);
- woodApp3.setTexture(woodTex3);
- stoneApp.setTextureAttributes(texAttr);
- Appearance ballApp = new Appearance();
- ballApp.setMaterial(ballMat);
- Appearance hitApp = new Appearance();
- //Ball Colors
- hitApp.setMaterial(new Material(white, black, white, white, 80.0f));
- Transform3D t = new Transform3D();
- t.set(1.0, new Vector3d(0.0, 0.0 ,BALL_RADIUS));
- TransformGroup ballTG = new TransformGroup(t);
- ballTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- ballTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
- TransformGroup blockTG = new TransformGroup();
- blockTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- blockTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
- TransformGroup hitTG = new TransformGroup();
- hitTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
- hitTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
- //
- TransformGroup[] pillarTG=new TransformGroup[4];
- Box[] pillar= new Box[4];
- t.set(1.0,new Vector3d(0,0,-PILLAR_HEIGHT/2));
- pillarTG[0] = new TransformGroup(t);
- pillar[0] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
- pillarTG[0].addChild(pillar[0]);
- t.set(1.0,new Vector3d(PATH_SIDE,0,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2));
- pillarTG[1] = new TransformGroup(t);
- pillar[1] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2,Box.GENERATE_TEXTURE_COORDS,stoneApp1);
- pillarTG[1].addChild(pillar[1]);
- t.set(1.0,new Vector3d(PATH_SIDE,PATH_SIDE,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2));
- pillarTG[2] = new TransformGroup(t);
- pillar[2] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
- pillarTG[2].addChild(pillar[2]);
- t.set(1.0,new Vector3d(0,PATH_SIDE,-PILLAR_HEIGHT/2));
- pillarTG[3] = new TransformGroup(t);
- pillar[3] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
- pillarTG[3].addChild(pillar[3]);
- TransformGroup[] woodTG=new TransformGroup[3];
- Box[] wood= new Box[3];
- // FAR LEFT SIDE OF TABLE
- t.set(1.0,new Vector3d(0,PATH_SIDE/2,-WOOD_THICK/2));
- woodTG[0] = new TransformGroup(t);
- wood[0]= new Box(PILLAR_WIDTH/2,PATH_SIDE/2-PILLAR_WIDTH/2,WOOD_THICK/2,Box.GENERATE_TEXTURE_COORDS,sideApp);
- woodTG[0].addChild(wood[0]);
- t=new Transform3D();
- t.set(1.0,new Vector3d(PATH_SIDE/2,0,HEIGHT_DIFFERENCE/2-WOOD_THICK/2));
- Transform3D helperT3D= new Transform3D();
- helperT3D.rotY(-Math.atan(HEIGHT_DIFFERENCE/(PATH_SIDE-PILLAR_WIDTH)));
- t.mul(helperT3D);
- helperT3D.rotZ(Math.PI/2);
- t.mul(helperT3D);
- // RIGHT SIDE OF THE TABLE
- woodTG[1] = new TransformGroup(t);
- wood[1]= new Box(PILLAR_WIDTH/2,(float) Math.sqrt(Math.pow(PATH_SIDE-PILLAR_WIDTH,2)+Math.pow(HEIGHT_DIFFERENCE,2))/2,WOOD_THICK/2,Box.GENERATE_TEXTURE_COORDS,woodApp2);
- woodTG[1].addChild(wood[1]);
- //LEFT SIDE
- t=new Transform3D();
- t.set(1.0,new Vector3d(PATH_SIDE/2,PATH_SIDE,HEIGHT_DIFFERENCE/2-WOOD_THICK/2));
- helperT3D= new Transform3D();
- helperT3D.rotY(-Math.atan(HEIGHT_DIFFERENCE/(PATH_SIDE-PILLAR_WIDTH)));
- t.mul(helperT3D);
- helperT3D.rotZ(Math.PI/2);
- t.mul(helperT3D);
- woodTG[2] = new TransformGroup(t);
- wood[2]= new Box(PILLAR_WIDTH/2,(float) Math.sqrt(Math.pow(PATH_SIDE-PILLAR_WIDTH,2)+Math.pow(HEIGHT_DIFFERENCE,2))/2,WOOD_THICK/2,Box.GENERATE_TEXTURE_COORDS,woodApp3);
- woodTG[2].addChild(wood[2]);
- //Sphere ball= new Sphere(BALL_RADIUS , ballApp);
- Alpha timingBall = new Alpha(-1, 12000);
- PositionPathInterpolator ppiBall = new PositionPathInterpolator(
- timingBall,ballTG,new Transform3D(),speed, path);
- ppiBall.setSchedulingBounds(bounds);
- Box block = new Box(PILLAR_WIDTH/2,PILLAR_WIDTH,PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
- Alpha timingBlock = new Alpha(-1,4000);
- PositionPathInterpolator ppiBlock = new PositionPathInterpolator(
- timingBlock,blockTG,new Transform3D(),speed2, path2);
- ppiBlock.setSchedulingBounds(bounds);
- Sphere hit = new Sphere(HIT_RADIUS, hitApp);
- t.set(1.0,new Vector3d(0,0,-HIT_LENGTH));
- TransformGroup hit2TG = new TransformGroup(t);
- t.set(1.0,new Vector3d(0,PATH_SIDE/2,HIT_LENGTH+HIT_RADIUS));
- TransformGroup hit0TG = new TransformGroup(t);
- hit0TG.addChild(hitTG);
- hitTG.addChild(hit2TG);
- hit2TG.addChild(hit);
- Alpha hitAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE |
- Alpha.DECREASING_ENABLE,
- 0, 0,
- 3500, 2500, 200,
- 3500, 2500, 200);
- Transform3D yAxis1 = new Transform3D();
- yAxis1.rotZ(Math.PI/2.0);
- RotationInterpolator hitRot =
- new RotationInterpolator(hitAlpha, hitTG, yAxis1,
- -(float) Math.PI/2.0f,
- (float) Math.PI/2.0f);
- hitRot.setSchedulingBounds(bounds);
- //ballTG.addChild(ball);
- ballTG.addChild(ballLgt);
- blockTG.addChild(block);
- CollisionBehavior hitB = new CollisionBehavior(hit);
- hitB.setSchedulingBounds(bounds);
- mainTG.addChild(ballTG);
- mainTG.addChild(hit0TG);
- mainTG.addChild(blockTG);
- mainTG.addChild(ppiBall);
- mainTG.addChild(ppiBlock);
- mainTG.addChild(hitRot);
- mainTG.addChild(hitB);
- // My Pocket
- mainTG.addChild(ballz);
- //mainTG.addChild(recto);
- for (int i=0;i<4; ++i)
- mainTG.addChild(pillarTG[i]);
- for (int i=0;i<3; ++i)
- mainTG.addChild(woodTG[i]);
- return mainTG;
- }
- public BranchGroup createSceneGraph(SimpleUniverse su) {
- // creating the universe
- BranchGroup objRoot = new BranchGroup();
- // adding the TG of the picture to the universe
- objRoot.addChild(createPicture());
- // adding mouse and keyboard controls
- addControls(su,objRoot);
- objRoot.compile();
- return objRoot;
- }
- public void addControls(SimpleUniverse su, BranchGroup bg) {
- TransformGroup cameraTG = su.getViewingPlatform().
- getViewPlatformTransform();
- BoundingSphere bounds=new BoundingSphere(new Point3d(),1000.0);
- // starting postion of the viewing platform
- Vector3f translate = new Vector3f();
- Transform3D T3D = new Transform3D();
- translate.set( 0.0f, 0.3f, 0.0f);
- T3D.setTranslation(translate);
- cameraTG.setTransform(T3D);
- // Create the key behavior node
- KeyNavigatorBehavior keyBehavior = new
- KeyNavigatorBehavior(cameraTG);
- keyBehavior.setSchedulingBounds(bounds);
- bg.addChild(keyBehavior);
- // Create the rotate behavior node
- MouseRotate behavior = new MouseRotate(MouseBehavior.INVERT_INPUT);
- behavior.setTransformGroup(cameraTG);
- behavior.setSchedulingBounds(bounds);
- bg.addChild(behavior);//
- // Create the zoom behavior node
- MouseZoom behavior2 = new MouseZoom(MouseBehavior.INVERT_INPUT);
- behavior2.setTransformGroup(cameraTG);
- behavior2.setSchedulingBounds(bounds);
- bg.addChild(behavior2);
- // Create the translate behavior node
- MouseTranslate behavior3 = new MouseTranslate(MouseBehavior.INVERT_INPUT);
- behavior3.setTransformGroup(cameraTG);
- behavior3.setSchedulingBounds(bounds);
- bg.addChild(behavior3);
- }
- public void init(){
- try{
- stoneURL = new URL( getCodeBase(),"Blackstone.jpg");
- stoneURL1 = new URL( getCodeBase(),"Blackstone1.jpg");
- woodURL= new URL(getCodeBase().toString() +"wood.gif");
- sideLEFT= new URL(getCodeBase().toString() +"wood1.gif");
- woodURL2= new URL(getCodeBase().toString() +"wood2.gif");
- woodURL3= new URL(getCodeBase().toString() +"wood3.gif");
- tableURL= new URL(getCodeBase().toString() +"table1.png");
- } catch(Exception e){
- e.printStackTrace();}
- }
- public Example3D() {
- Container cp = getContentPane();
- cp.setLayout(new BorderLayout());
- Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration() );
- cp.add("Center", c);
- SimpleUniverse u = new SimpleUniverse(c);
- BranchGroup scene = createSceneGraph(u);
- u.addBranchGraph(scene);
- }
- public static void main(String[] args) {
- try{
- stoneURL = new URL("file:stone.jpg");
- stoneURL1 = new URL("file:stone1.jpg");
- woodURL = new URL("file:wood.gif");
- sideLEFT = new URL("file:wood1.gif");
- woodURL2 = new URL("file:wood2.gif");
- woodURL3 = new URL("file:wood3.gif");
- tableURL = new URL("file:table1.png");
- } catch (Exception e){}
- new MainFrame(new Example3D(), 512, 512);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement