jotto

Untitled

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