jotto

Untitled

Jun 10th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.85 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import com.sun.j3d.utils.applet.MainFrame;
  5. import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
  6. import com.sun.j3d.utils.geometry.GeometryInfo;
  7. import com.sun.j3d.utils.geometry.Cylinder;
  8. import com.sun.j3d.utils.geometry.Sphere;
  9. import com.sun.j3d.utils.geometry.NormalGenerator;
  10. import com.sun.j3d.utils.universe.*;
  11. import java.util.Enumeration;
  12. import javax.media.j3d.*;
  13. import javax.vecmath.*;
  14. import java.util.Timer;
  15. import java.util.TimerTask;
  16. import javax.swing.JLabel;
  17.  
  18.  
  19. /**
  20. * Klasa główna 'Main' zawiera deklaracje zmiennych
  21. * @author
  22. */
  23. public final class Main extends Applet implements KeyListener{
  24.  
  25. final private static int Width = 900;
  26. final private static int Height = 600;
  27. final private OrbitBehavior obserwator;
  28. final private ViewingPlatform vPlatform;
  29. private RotationInterpolator punkt1; // arm sweep - baza robota
  30. private RotationInterpolator punkt2; // shoulder - ramie robota
  31. private RotationInterpolator punkt3;
  32. final private Timer zegar = new Timer();
  33. final private SimpleUniverse universe;
  34. private double x = 0.0f;
  35. private double y = 0.65f;
  36. private double z = 0.5f;
  37. private boolean spacja = false;
  38. private boolean zatrzask = false;
  39. private boolean inCollision = false;
  40. JLabel label = new JLabel("Ramię Robota:");
  41. private TransformGroup objTrans;
  42. private TransformGroup objTrans2;
  43. private TransformGroup objTrans3;
  44. private TransformGroup objTrans4;
  45. final private Transform3D trans = new Transform3D();
  46. final private Transform3D trans2 = new Transform3D();
  47. final private Transform3D trans3 = new Transform3D();
  48. final private Transform3D trans4 = new Transform3D();
  49. final private Transform3D commonRotation = new Transform3D();
  50.  
  51.  
  52. /**
  53. * W metodzie tworzymy scenę oraz elementy, które będą w niej zawarte - tj.
  54. * elementy manipulatora cylindrycznego (cylinder, cylinder2, chwytak i przedmiot),
  55. * oświetlenie sceny oraz podłoże służące jako punkt odniesienia.
  56. * @return obiekt 'scena' typu BranchGroup
  57. */
  58. public BranchGroup createSceneGraph() {
  59.  
  60. Alpha alpha1 = new Alpha(-1, 5000); // arm sweep
  61.  
  62. BranchGroup scena = new BranchGroup(); // scena główna - swiatla, podloga
  63. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  64.  
  65. Transform3D tmp = new Transform3D();
  66.  
  67.  
  68. objTrans = new TransformGroup();
  69. objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  70. objTrans2 = new TransformGroup();
  71. objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  72.  
  73. //statyw
  74. scena.addChild(objTrans);
  75. Cylinder cylinder1 = new Cylinder(0.1f, 1.5f);
  76. objTrans = new TransformGroup();
  77. objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  78. Transform3D pos1 = new Transform3D();
  79. pos1.setTranslation(new Vector3f(0.0f, 0.25f, 0.0f));
  80. objTrans.setTransform(pos1);
  81. objTrans.addChild(cylinder1);
  82. //scena.addChild(objTrans);
  83.  
  84. //ramie
  85. scena.addChild(objTrans2);
  86. Cylinder cylinder2 = new Cylinder(0.05f, 1.25f);
  87. objTrans2 = new TransformGroup();
  88. objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  89. objTrans2.addChild(cylinder2);
  90.  
  91. //kulka
  92. scena.addChild(objTrans3);
  93. Sphere chwytak = new Sphere(0.08f);
  94. objTrans3 = new TransformGroup();
  95. objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  96. objTrans3.addChild(chwytak);
  97. scena.addChild(objTrans3);
  98.  
  99. //kulka
  100. scena.addChild(objTrans4);
  101. Appearance app = new Appearance();
  102.  
  103. Material mat = new Material(
  104. new Color3f(.0f,.0f,.0f),
  105. new Color3f(.0f,.0f,.0f),
  106. new Color3f(0.4f,0.4f,0.4f),
  107. new Color3f(0.7f,0.7f,0.7f),
  108. 64);
  109. app.setColoringAttributes(new ColoringAttributes(1.0f, 0.0f, 0.0f,ColoringAttributes.ALLOW_COLOR_WRITE|ColoringAttributes.SHADE_GOURAUD));
  110. app.setMaterial(mat);
  111.  
  112.  
  113.  
  114. app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE);
  115. app.setCapability(app.ALLOW_MATERIAL_WRITE);
  116. Sphere przedmiot = new Sphere(0.1f, app);
  117. objTrans4 = new TransformGroup();
  118. objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  119. Transform3D kulka2 = new Transform3D();
  120. kulka2.setTranslation(new Vector3f(0.0f, 0.0f, 1.0f));
  121. objTrans4.setTransform(kulka2);
  122. objTrans4.addChild(przedmiot);
  123. scena.addChild(objTrans4);
  124.  
  125. // podłoże
  126. Appearance ap = new Appearance();
  127. ap.setMaterial(new Material(
  128. new Color3f(0.8f,.8f,.8f),
  129. new Color3f(.0f,.0f,.0f),
  130. new Color3f(0.8f,.8f,.8f),
  131. new Color3f(.0f,.0f,.0f),
  132. 80f
  133. ));
  134. podloga ob = new podloga();
  135. ob.setAppearance(ap);
  136. scena.addChild(ob);
  137.  
  138. // światło kierunkowe
  139. Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
  140. Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
  141. DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
  142. light1.setInfluencingBounds(bounds);
  143. scena.addChild(light1);
  144.  
  145. // światło
  146. Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
  147. AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  148. ambientLightNode.setInfluencingBounds(bounds);
  149. scena.addChild(ambientLightNode);
  150.  
  151. //objTrans2.addChild(objTrans3);
  152. objTrans.addChild(objTrans2);
  153. scena.addChild(objTrans);
  154.  
  155. // Create a new Behavior object that will perform the collision
  156. // detection on the specified object, and add it into
  157. // the scene graph.
  158. CollisionDetector cd = new CollisionDetector(przedmiot);
  159. BoundingSphere boundy = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10.0);
  160. cd.setSchedulingBounds(boundy);
  161. objTrans3.addChild(cd);
  162.  
  163. scena.compile();
  164. return scena;
  165. }
  166.  
  167.  
  168. /**
  169. * Konstruktor bezparametrowy klasy 'Main'. Ustawia on wygląd okna programu
  170. * (BorderLayout()), tworzy w nim podstawowe obiekty Java3D (SimpleUniverse).
  171. * Ustawia pozycję i zachowanie kamery oraz uruchamia zegar.
  172. */
  173. public Main() {
  174. setLayout(new BorderLayout());
  175.  
  176. GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  177. Canvas3D canvas3D = new Canvas3D(config);
  178. add("Center", canvas3D);
  179. canvas3D.addKeyListener(this);
  180. add("South",label);
  181.  
  182. // Create a simple scene and attach it to the virtual universe
  183. BranchGroup scene = createSceneGraph();
  184. universe = new SimpleUniverse(canvas3D);
  185. universe.addBranchGraph(scene);
  186. obserwator = new OrbitBehavior(canvas3D);
  187. BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  188. obserwator.setSchedulingBounds(bounds);
  189. vPlatform = universe.getViewingPlatform();
  190. Transform3D temp = new Transform3D();
  191. temp.set(new Vector3f(0f,0f,7.0f));
  192. vPlatform.getViewPlatformTransform().setTransform(temp);
  193. vPlatform.setViewPlatformBehavior(obserwator);
  194. zegar.scheduleAtFixedRate(new Ruch(), 20, 25);
  195. }
  196.  
  197. /**
  198. * Funkcja tworzy okno typu MainFrame o zadanych wymiarach Width i Height.
  199. * Konstruktor okna wywołuje konstruktor klasy Main().
  200. * @param args
  201. */
  202. public static void main(String[] args) {
  203. //Main bb = new Main();
  204. MainFrame mf = new MainFrame(new Main(),Width, Height);
  205. }
  206.  
  207. /******************************************************************************/
  208.  
  209. class CollisionDetector extends Behavior {
  210. private final Color3f highlightColor = new Color3f(0.0f, 1.0f, 0.0f);
  211.  
  212. private final ColoringAttributes highlight = new ColoringAttributes(
  213. highlightColor, ColoringAttributes.SHADE_GOURAUD);
  214.  
  215. private final Material hajlajt = new Material (
  216. new Color3f(.15f, .15f, .15f),
  217. new Color3f(0.4f, 0.0f, 0.0f),
  218. new Color3f(0.4f,0.4f,0.4f),
  219. new Color3f(0.7f,0.7f,0.7f),
  220. 64
  221. );
  222.  
  223. private Sphere sferka;
  224.  
  225. private ColoringAttributes shapeColoring;
  226.  
  227. private Appearance shapeAppearance;
  228.  
  229. private Material shapeMaterial;
  230.  
  231. private WakeupOnCollisionEntry wEnter;
  232.  
  233. private WakeupOnCollisionExit wExit;
  234.  
  235. public CollisionDetector(Sphere s) {
  236. sferka = s;
  237. shapeAppearance = sferka.getAppearance();
  238. shapeColoring = shapeAppearance.getColoringAttributes();
  239. shapeMaterial = shapeAppearance.getMaterial();
  240. inCollision = false;
  241. }
  242.  
  243. public void initialize() {
  244. wEnter = new WakeupOnCollisionEntry(sferka);
  245. wExit = new WakeupOnCollisionExit(sferka);
  246. wakeupOn(wEnter);
  247. }
  248.  
  249. public void processStimulus(Enumeration criteria) {
  250. inCollision = !inCollision;
  251.  
  252. if (inCollision) {
  253. shapeAppearance.setColoringAttributes(highlight);
  254. shapeAppearance.setMaterial(hajlajt);
  255. wakeupOn(wExit);
  256. } else {
  257. shapeAppearance.setColoringAttributes(shapeColoring);
  258. shapeAppearance.setMaterial(shapeMaterial);
  259. wakeupOn(wEnter);
  260. }
  261. }
  262. }
  263.  
  264. /******************************************************************************/
  265.  
  266. public static float round (double f)
  267.  
  268. { float temp = (float)(f*(Math.pow(10, 0)));
  269.  
  270. temp = (Math.round(temp));
  271.  
  272. temp = temp/(int)(Math.pow(10, 0));
  273.  
  274. return temp;
  275.  
  276. }
  277. /**
  278. * Funkcja wychwytuje zdarzenie wciśniecią przycisku na klawiaturze.
  279. * Zawiera obsługę przycisków służących do manipulacji robotem.
  280. * UP, DOWN - przesuw ramienia w pionie
  281. * LEFT, RIGHT - obrót ramienia wokół jego osi pionowej
  282. * A, S - przesuw ramienia w poziomie (wysuwanie, wsuwanie)
  283. * Z - standardowe ustawienie kamery
  284. * @param e (KeyEvent)
  285. */
  286. @Override
  287. public void keyPressed(KeyEvent e) {
  288. if(e.getKeyCode() == KeyEvent.VK_Z){
  289. vPlatform.setNominalViewingTransform();
  290. }
  291. if (Math.round(Math.toDegrees(x))==360) x=Math.toRadians(0);
  292. if (Math.toDegrees(x)==-1) x=Math.toRadians(358.9999999999974);
  293. switch(e.getKeyCode()){
  294. case KeyEvent.VK_UP:
  295. if(y < 0.65f) {
  296. y += 0.05;
  297. }
  298. break;
  299. case KeyEvent.VK_DOWN:
  300. if(y > -0.6f) {
  301. y -= 0.05;
  302. }
  303. break;
  304. case KeyEvent.VK_LEFT:
  305. x -= Math.PI/180;
  306. break;
  307. case KeyEvent.VK_RIGHT:
  308. x += Math.PI/180;
  309. break;
  310. case KeyEvent.VK_A:
  311. if(z > -0.3f) {
  312. z -= 0.05;
  313. }
  314. break;
  315. case KeyEvent.VK_S:
  316. if(z < 0.5f) {
  317. z += 0.05;
  318. }
  319. break;
  320. case KeyEvent.VK_SPACE:
  321. spacja = !spacja;
  322. zatrzask = true;
  323. }
  324. }
  325.  
  326. /**
  327. * Funkcja wychwytująca puszczenie przycisku klawiatury - nie używana.
  328. * @param e
  329. */
  330. @Override
  331. public void keyReleased(KeyEvent e){
  332. switch(e.getKeyCode()){
  333. case KeyEvent.VK_SPACE:
  334. zatrzask = false;
  335. break;
  336. }
  337. }
  338.  
  339. /**
  340. * Funkcja wychwytująca sygnał przycisku klawiatury - nie używana.
  341. * @param e
  342. */
  343. @Override
  344. public void keyTyped(KeyEvent e){
  345. }
  346.  
  347.  
  348. // @Override
  349. // public void mouseMoved(MouseEvent e) {
  350. // // x = (float)(e.getX())/Width-.5f;
  351. // //y = -(float)(e.getY())/Height+.5f;
  352. // }
  353.  
  354. /**
  355. * Klasa dziedzicząca po klasie TimerTask.
  356. * Zawiera funkcję run().
  357. */
  358. private class Ruch extends TimerTask{
  359. /**
  360. * Zadaniem funkcji jest przekształcanie
  361. * sceny w zależności od parametrów (zmienne globalne) oraz wyświetlanie
  362. * informacji o aktualnych wartościach parametrów w dolnym pasku
  363. * informacyjnym.
  364. */
  365. @Override
  366. public void run() {
  367. commonRotation.rotY(x);
  368.  
  369. trans2.rotX(Math.PI/2);
  370. trans2.setTranslation(new Vector3d(0.0f, y, z));
  371. trans2.mul(commonRotation, trans2);
  372. objTrans2.setTransform(trans2);
  373.  
  374. trans3.setTranslation(new Vector3d(0.0f,y+0.25,z+0.6));
  375. trans3.mul(commonRotation, trans3);
  376. objTrans3.setTransform(trans3);
  377.  
  378.  
  379.  
  380. trans4.setTranslation(new Vector3d(0.0f, y+0.25, z+0.64));
  381. trans4.mul(commonRotation, trans4);
  382. if (spacja==true && inCollision==true) {
  383. objTrans4.setTransform(trans4);
  384. }
  385. label.setText("Współrzędne: " + Math.round(Math.toDegrees(x)) + " , "+ y + " , " + z);
  386. }
  387. }
  388.  
  389. /**
  390. * Klasa 'podloga' dziedzicząca po klasie 'Shape3D'.
  391. */
  392. public class podloga extends Shape3D{ // podłoże robota
  393. final private Point3f A= new Point3f(-5.0f, -0.5f, -5.0f);
  394. final private Point3f B= new Point3f(-5.0f, -0.5f, 5.0f);
  395. final private Point3f C= new Point3f(5.0f, -0.5f, 5.0f);
  396. final private Point3f D= new Point3f(5.0f, -0.5f, -5.0f);
  397. final private Point3f[] pts = new Point3f[8];
  398. int[] stripCounts= new int[2];
  399. int[] contourCount=new int[2];
  400.  
  401. /**
  402. * Konstruktor bezparametrowy klasy 'podloga'. Tworzy punkt odniesienia
  403. * dla manipulatora.
  404. */
  405. public podloga(){
  406. // front
  407. pts[0]=C;
  408. pts[1]=D;
  409. pts[2]=A;
  410. pts[3]=B;
  411. //back
  412. pts[4]=C;
  413. pts[5]=B;
  414. pts[6]=A;
  415. pts[7]=D;
  416.  
  417. stripCounts[0]=4;
  418. stripCounts[1]=4;
  419. contourCount[0]=1;
  420. contourCount[1]=1;
  421. GeometryInfo gInf = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
  422. gInf.setCoordinates(pts);
  423. gInf.setStripCounts(stripCounts);
  424. gInf.setContourCounts(contourCount);
  425. NormalGenerator ng= new NormalGenerator();
  426. ng.setCreaseAngle ((float) Math.toRadians(30));
  427. ng.generateNormals(gInf);
  428. this.setGeometry(gInf.getGeometryArray());
  429. }
  430. }
  431.  
  432. }
Advertisement
Add Comment
Please, Sign In to add comment