Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.37 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.event.*;
  3. import com.sun.j3d.utils.applet.MainFrame;
  4. import com.sun.j3d.utils.geometry.*;
  5. import com.sun.j3d.utils.universe.*;
  6. import javax.media.j3d.*;
  7. import javax.vecmath.*;
  8. import java.io.*;
  9. import java.awt.*;
  10.  
  11. import com.sun.j3d.utils.behaviors.mouse.*;
  12. import com.sun.j3d.utils.behaviors.keyboard.*;
  13. import javax.swing.JApplet;
  14. import com.sun.j3d.utils.image.TextureLoader;
  15. import java.net.*;
  16. /**
  17. * Describe class Example3D here.
  18. *
  19. *
  20. * Created: Sun Jan 11 00:50:05 2010
  21. *
  22. * @author <a href="mailto:">M. Hoffmann</a>
  23. * @version 1.0
  24. */
  25. public class Example3D extends JApplet {
  26.  
  27. static public final Color3f green = new Color3f(0.0f,1.0f,0.0f);
  28. static public final Color3f red = new Color3f(1.0f,0.0f,0.0f);
  29. static public final Color3f white = new Color3f(1.0f,1.0f,1.0f);
  30. static public final Color3f gray = new Color3f(.5f,.50f,.50f);
  31. static public final Color3f black = new Color3f(0.0f,0.0f,0.0f);
  32. static public final Color3f blue = new Color3f(0.0f,0.0f,1.0f);
  33.  
  34. //
  35. static public final Rectangle recto = new Rectangle();
  36.  
  37. //Pocket
  38.  
  39. static public final Sphere ballz = new Sphere(0.5f);
  40.  
  41.  
  42. static public final float PATH_SIDE =15f;
  43. static public final float PATH_SIDE1 =30f;
  44.  
  45.  
  46. static public final float PILLAR_WIDTH =1.5f;
  47. static public final float PILLAR_HEIGHT=0.5f;
  48.  
  49.  
  50. //
  51. static public final float PILLAR_WIDTH1 =8f;
  52. static public final float PILLAR_HEIGHT1=9f;
  53.  
  54. static public final float BALL_RADIUS =1f;
  55. static public final float HIT_RADIUS =1f;
  56. static public final float HIT_LENGTH =8f;
  57.  
  58. //Size Of Pillars
  59. static public final float HEIGHT_DIFFERENCE = 0.5f;
  60.  
  61. //THICKNESS OF SIDES
  62. static public final float WOOD_THICK = .4f;
  63.  
  64. //SPEED OF BALL
  65. static public final float[] speed = {0.0f,.25f,.5f,.75f,1f};
  66. static public final float[] speed2 = {0.0f,.25f,.5f,.75f,1f};
  67.  
  68. //PATHS
  69.  
  70. private Point3f[] path ;
  71. private Point3f[] path2 ;
  72.  
  73. //TEXTURE URLS
  74. private static URL stoneURL, stoneURL1, woodURL, sideLEFT, woodURL2, woodURL3 ,tableURL;
  75.  
  76. public TransformGroup createPicture() {
  77.  
  78. //POINTS FOR THE BALL
  79. path=new Point3f[5];
  80. path[0]=new Point3f(0f,0f,BALL_RADIUS);
  81. path[1]=new Point3f(PATH_SIDE,0f,BALL_RADIUS+HEIGHT_DIFFERENCE);
  82. path[2]=new Point3f(PATH_SIDE,PATH_SIDE,BALL_RADIUS+HEIGHT_DIFFERENCE);
  83. path[3]=new Point3f(0f,PATH_SIDE,BALL_RADIUS);
  84. path[4]=new Point3f(0f,0f,BALL_RADIUS);
  85.  
  86. // THE POINTS
  87. path2=new Point3f[5];
  88. // THE POINT OF THE MOVE PILLAR
  89. path2[0]=new Point3f(4,PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
  90. //
  91. path2[1]=new Point3f(9,PATH_SIDE-PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
  92. path2[2]=new Point3f(PATH_SIDE,PATH_SIDE-PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
  93. path2[3]=new Point3f(PATH_SIDE,PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
  94. path2[4]=new Point3f(PATH_SIDE,PILLAR_WIDTH*1.5f,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2);
  95. Texture woodTex=null,stoneTex=null, stoneTex1=null, woodTex1=null, woodTex2=null, woodTex3=null,tableTex = null;
  96.  
  97. // Load Textures
  98. stoneTex = new TextureLoader(stoneURL, this).getTexture();
  99. stoneTex1 = new TextureLoader(stoneURL1, this).getTexture();
  100.  
  101. woodTex = new TextureLoader(woodURL, this).getTexture();
  102. woodTex1 = new TextureLoader(sideLEFT, this).getTexture();
  103. woodTex2 = new TextureLoader(woodURL2, this).getTexture();
  104. woodTex3 = new TextureLoader(woodURL3, this).getTexture();
  105. tableTex = new TextureLoader(tableURL,this).getTexture();
  106. // creating the universe
  107. BoundingSphere bounds =
  108. new BoundingSphere(new Point3d(0.0,0.0,0.0), 1000.0);
  109.  
  110. TransformGroup mainTG = new TransformGroup();
  111.  
  112. AmbientLight aLgt = new AmbientLight(gray);
  113. aLgt.setInfluencingBounds(bounds);
  114.  
  115. DirectionalLight lgt1 = new DirectionalLight(gray, new Vector3f(-1.0f, -1.0f, -1.0f));
  116. lgt1.setInfluencingBounds(bounds);
  117.  
  118. DirectionalLight ballLgt = new DirectionalLight(red, new Vector3f(-1.0f, -1.0f, -1.0f));
  119. ballLgt.setInfluencingBounds(bounds);
  120.  
  121. // objRoot.addChild(aLgt);
  122. mainTG.addChild(lgt1);
  123.  
  124.  
  125. Appearance stoneApp = new Appearance();
  126. Appearance stoneApp1 = new Appearance();
  127.  
  128.  
  129. stoneApp.setTexture(stoneTex);
  130. stoneApp1.setTexture(stoneTex1);
  131.  
  132. TextureAttributes texAttr = new TextureAttributes();
  133. texAttr.setTextureMode(TextureAttributes.MODULATE);
  134. stoneApp.setTextureAttributes(texAttr);
  135. stoneApp.setMaterial(new Material(white, black, white, black, 100.0f));
  136.  
  137.  
  138. Material ballMat=new Material(red, black, red, white, 100.0f);
  139. ballMat.setLightingEnable(true);
  140. // stoneApp.setMaterial(ballMat);
  141.  
  142.  
  143. //WOODEN BLOCK
  144.  
  145. Appearance woodApp = new Appearance();
  146. Appearance sideApp = new Appearance();
  147. Appearance woodApp2 = new Appearance();
  148. Appearance woodApp3 = new Appearance();
  149.  
  150. woodApp.setTexture(woodTex);
  151. sideApp.setTexture(woodTex1);
  152. woodApp2.setTexture(woodTex2);
  153. woodApp3.setTexture(woodTex3);
  154.  
  155.  
  156.  
  157. stoneApp.setTextureAttributes(texAttr);
  158.  
  159. Appearance ballApp = new Appearance();
  160. ballApp.setMaterial(ballMat);
  161.  
  162. Appearance hitApp = new Appearance();
  163.  
  164. //Ball Colors
  165. hitApp.setMaterial(new Material(white, black, white, white, 80.0f));
  166.  
  167. Transform3D t = new Transform3D();
  168. t.set(1.0, new Vector3d(0.0, 0.0 ,BALL_RADIUS));
  169.  
  170. TransformGroup ballTG = new TransformGroup(t);
  171. ballTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  172. ballTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  173.  
  174. TransformGroup blockTG = new TransformGroup();
  175. blockTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  176. blockTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  177.  
  178. TransformGroup hitTG = new TransformGroup();
  179. hitTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  180. hitTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  181.  
  182.  
  183. //
  184. TransformGroup[] pillarTG=new TransformGroup[4];
  185. Box[] pillar= new Box[4];
  186.  
  187. t.set(1.0,new Vector3d(0,0,-PILLAR_HEIGHT/2));
  188. pillarTG[0] = new TransformGroup(t);
  189. pillar[0] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
  190. pillarTG[0].addChild(pillar[0]);
  191.  
  192. t.set(1.0,new Vector3d(PATH_SIDE,0,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2));
  193. pillarTG[1] = new TransformGroup(t);
  194. pillar[1] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2,Box.GENERATE_TEXTURE_COORDS,stoneApp1);
  195. pillarTG[1].addChild(pillar[1]);
  196.  
  197. t.set(1.0,new Vector3d(PATH_SIDE,PATH_SIDE,-PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2));
  198. pillarTG[2] = new TransformGroup(t);
  199. pillar[2] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
  200. pillarTG[2].addChild(pillar[2]);
  201.  
  202. t.set(1.0,new Vector3d(0,PATH_SIDE,-PILLAR_HEIGHT/2));
  203. pillarTG[3] = new TransformGroup(t);
  204. pillar[3] = new Box(PILLAR_WIDTH/2, PILLAR_WIDTH/2,PILLAR_HEIGHT/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
  205. pillarTG[3].addChild(pillar[3]);
  206.  
  207.  
  208.  
  209.  
  210. TransformGroup[] woodTG=new TransformGroup[3];
  211. Box[] wood= new Box[3];
  212.  
  213.  
  214. // FAR LEFT SIDE OF TABLE
  215. t.set(1.0,new Vector3d(0,PATH_SIDE/2,-WOOD_THICK/2));
  216. woodTG[0] = new TransformGroup(t);
  217. wood[0]= new Box(PILLAR_WIDTH/2,PATH_SIDE/2-PILLAR_WIDTH/2,WOOD_THICK/2,Box.GENERATE_TEXTURE_COORDS,sideApp);
  218. woodTG[0].addChild(wood[0]);
  219.  
  220.  
  221. t=new Transform3D();
  222. t.set(1.0,new Vector3d(PATH_SIDE/2,0,HEIGHT_DIFFERENCE/2-WOOD_THICK/2));
  223. Transform3D helperT3D= new Transform3D();
  224. helperT3D.rotY(-Math.atan(HEIGHT_DIFFERENCE/(PATH_SIDE-PILLAR_WIDTH)));
  225. t.mul(helperT3D);
  226. helperT3D.rotZ(Math.PI/2);
  227. t.mul(helperT3D);
  228.  
  229. // RIGHT SIDE OF THE TABLE
  230. woodTG[1] = new TransformGroup(t);
  231. 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);
  232. woodTG[1].addChild(wood[1]);
  233.  
  234.  
  235.  
  236. //LEFT SIDE
  237. t=new Transform3D();
  238. t.set(1.0,new Vector3d(PATH_SIDE/2,PATH_SIDE,HEIGHT_DIFFERENCE/2-WOOD_THICK/2));
  239. helperT3D= new Transform3D();
  240. helperT3D.rotY(-Math.atan(HEIGHT_DIFFERENCE/(PATH_SIDE-PILLAR_WIDTH)));
  241. t.mul(helperT3D);
  242. helperT3D.rotZ(Math.PI/2);
  243. t.mul(helperT3D);
  244. woodTG[2] = new TransformGroup(t);
  245. 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);
  246. woodTG[2].addChild(wood[2]);
  247.  
  248. //Sphere ball= new Sphere(BALL_RADIUS , ballApp);
  249.  
  250.  
  251. Alpha timingBall = new Alpha(-1, 12000);
  252.  
  253. PositionPathInterpolator ppiBall = new PositionPathInterpolator(
  254. timingBall,ballTG,new Transform3D(),speed, path);
  255. ppiBall.setSchedulingBounds(bounds);
  256.  
  257. Box block = new Box(PILLAR_WIDTH/2,PILLAR_WIDTH,PILLAR_HEIGHT/2+HEIGHT_DIFFERENCE/2,Box.GENERATE_TEXTURE_COORDS,stoneApp);
  258. Alpha timingBlock = new Alpha(-1,4000);
  259. PositionPathInterpolator ppiBlock = new PositionPathInterpolator(
  260. timingBlock,blockTG,new Transform3D(),speed2, path2);
  261. ppiBlock.setSchedulingBounds(bounds);
  262.  
  263.  
  264. Sphere hit = new Sphere(HIT_RADIUS, hitApp);
  265. t.set(1.0,new Vector3d(0,0,-HIT_LENGTH));
  266. TransformGroup hit2TG = new TransformGroup(t);
  267.  
  268. t.set(1.0,new Vector3d(0,PATH_SIDE/2,HIT_LENGTH+HIT_RADIUS));
  269. TransformGroup hit0TG = new TransformGroup(t);
  270.  
  271.  
  272. hit0TG.addChild(hitTG);
  273. hitTG.addChild(hit2TG);
  274. hit2TG.addChild(hit);
  275.  
  276. Alpha hitAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE |
  277. Alpha.DECREASING_ENABLE,
  278. 0, 0,
  279. 3500, 2500, 200,
  280. 3500, 2500, 200);
  281.  
  282. Transform3D yAxis1 = new Transform3D();
  283. yAxis1.rotZ(Math.PI/2.0);
  284. RotationInterpolator hitRot =
  285. new RotationInterpolator(hitAlpha, hitTG, yAxis1,
  286. -(float) Math.PI/2.0f,
  287. (float) Math.PI/2.0f);
  288. hitRot.setSchedulingBounds(bounds);
  289.  
  290.  
  291. //ballTG.addChild(ball);
  292. ballTG.addChild(ballLgt);
  293. blockTG.addChild(block);
  294.  
  295.  
  296. CollisionBehavior hitB = new CollisionBehavior(hit);
  297. hitB.setSchedulingBounds(bounds);
  298.  
  299.  
  300. mainTG.addChild(ballTG);
  301. mainTG.addChild(hit0TG);
  302. mainTG.addChild(blockTG);
  303. mainTG.addChild(ppiBall);
  304. mainTG.addChild(ppiBlock);
  305. mainTG.addChild(hitRot);
  306. mainTG.addChild(hitB);
  307.  
  308.  
  309. // My Pocket
  310. mainTG.addChild(ballz);
  311. //mainTG.addChild(recto);
  312. for (int i=0;i<4; ++i)
  313. mainTG.addChild(pillarTG[i]);
  314.  
  315. for (int i=0;i<3; ++i)
  316. mainTG.addChild(woodTG[i]);
  317.  
  318. return mainTG;
  319. }
  320.  
  321.  
  322. public BranchGroup createSceneGraph(SimpleUniverse su) {
  323. // creating the universe
  324. BranchGroup objRoot = new BranchGroup();
  325.  
  326. // adding the TG of the picture to the universe
  327. objRoot.addChild(createPicture());
  328.  
  329. // adding mouse and keyboard controls
  330. addControls(su,objRoot);
  331.  
  332. objRoot.compile();
  333.  
  334. return objRoot;
  335. }
  336.  
  337. public void addControls(SimpleUniverse su, BranchGroup bg) {
  338. TransformGroup cameraTG = su.getViewingPlatform().
  339. getViewPlatformTransform();
  340. BoundingSphere bounds=new BoundingSphere(new Point3d(),1000.0);
  341. // starting postion of the viewing platform
  342. Vector3f translate = new Vector3f();
  343. Transform3D T3D = new Transform3D();
  344. translate.set( 0.0f, 0.3f, 0.0f);
  345. T3D.setTranslation(translate);
  346. cameraTG.setTransform(T3D);
  347.  
  348. // Create the key behavior node
  349. KeyNavigatorBehavior keyBehavior = new
  350. KeyNavigatorBehavior(cameraTG);
  351. keyBehavior.setSchedulingBounds(bounds);
  352. bg.addChild(keyBehavior);
  353.  
  354. // Create the rotate behavior node
  355. MouseRotate behavior = new MouseRotate(MouseBehavior.INVERT_INPUT);
  356. behavior.setTransformGroup(cameraTG);
  357. behavior.setSchedulingBounds(bounds);
  358. bg.addChild(behavior);//
  359.  
  360. // Create the zoom behavior node
  361. MouseZoom behavior2 = new MouseZoom(MouseBehavior.INVERT_INPUT);
  362. behavior2.setTransformGroup(cameraTG);
  363. behavior2.setSchedulingBounds(bounds);
  364. bg.addChild(behavior2);
  365.  
  366.  
  367. // Create the translate behavior node
  368. MouseTranslate behavior3 = new MouseTranslate(MouseBehavior.INVERT_INPUT);
  369. behavior3.setTransformGroup(cameraTG);
  370. behavior3.setSchedulingBounds(bounds);
  371. bg.addChild(behavior3);
  372. }
  373.  
  374. public void init(){
  375. try{
  376. stoneURL = new URL( getCodeBase(),"Blackstone.jpg");
  377. stoneURL1 = new URL( getCodeBase(),"Blackstone1.jpg");
  378.  
  379. woodURL= new URL(getCodeBase().toString() +"wood.gif");
  380. sideLEFT= new URL(getCodeBase().toString() +"wood1.gif");
  381. woodURL2= new URL(getCodeBase().toString() +"wood2.gif");
  382. woodURL3= new URL(getCodeBase().toString() +"wood3.gif");
  383. tableURL= new URL(getCodeBase().toString() +"table1.png");
  384.  
  385.  
  386.  
  387. } catch(Exception e){
  388. e.printStackTrace();}
  389. }
  390.  
  391.  
  392. public Example3D() {
  393. Container cp = getContentPane();
  394. cp.setLayout(new BorderLayout());
  395. Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration() );
  396. cp.add("Center", c);
  397. SimpleUniverse u = new SimpleUniverse(c);
  398. BranchGroup scene = createSceneGraph(u);
  399. u.addBranchGraph(scene);
  400. }
  401.  
  402. public static void main(String[] args) {
  403. try{
  404. stoneURL = new URL("file:stone.jpg");
  405. stoneURL1 = new URL("file:stone1.jpg");
  406.  
  407. woodURL = new URL("file:wood.gif");
  408. sideLEFT = new URL("file:wood1.gif");
  409. woodURL2 = new URL("file:wood2.gif");
  410. woodURL3 = new URL("file:wood3.gif");
  411. tableURL = new URL("file:table1.png");
  412.  
  413. } catch (Exception e){}
  414. new MainFrame(new Example3D(), 512, 512);
  415. }
  416.  
  417. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement