jotto

Untitled

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