jotto

Untitled

Jun 10th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 x2 = 0.1f;
  38.     private double y2 = 0.9f;
  39.     private double z2 = 1.1f;
  40.     private boolean spacja = false;
  41.     private boolean zatrzask = false;
  42.     private boolean inCollision = false;
  43.     JLabel label = new JLabel("Ramię Robota:");
  44.     private TransformGroup objTrans;
  45.     final private Transform3D trans = new Transform3D();
  46.     private TransformGroup objTrans2;
  47.     final private Transform3D trans2 = new Transform3D();
  48.     private TransformGroup objTrans3;
  49.     final private Transform3D trans3 = new Transform3D();
  50.     private TransformGroup objTrans4;
  51.     final private Transform3D trans4 = new Transform3D();
  52.     private RotationInterpolator rotacja;
  53.     final private Transform3D hujRotation = 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.        Transform3D cyl2 = new Transform3D();
  94.        tmp.set(new Vector3f(0.0f,0.0f,0.0f));
  95.        cyl2.rotX(Math.PI/2);
  96.        tmp.mul(cyl2);
  97.        cyl2.setTranslation(new Vector3d(x, y, z));
  98.        objTrans2.setTransform(cyl2);
  99.        objTrans2.addChild(cylinder2);
  100.        //scena.addChild(objTrans2);
  101.        
  102.        //kulka
  103.        scena.addChild(objTrans3);
  104.        Sphere chwytak = new Sphere(0.08f);
  105.        objTrans3 = new TransformGroup();
  106.        objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  107.        Transform3D kulka = new Transform3D();
  108.        //kulka.setTranslation(new Vector3d(0.0, y2, z2));
  109.        objTrans3.setTransform(kulka);
  110.        objTrans3.addChild(chwytak);
  111.        scena.addChild(objTrans3);
  112.        
  113.        //kulka
  114.        scena.addChild(objTrans4);
  115.        
  116.        Appearance app = new Appearance();
  117.        app.setColoringAttributes(new ColoringAttributes(1.0f, 0.0f, 0.0f,ColoringAttributes.ALLOW_COLOR_WRITE));
  118.        app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE);
  119.        Sphere przedmiot = new Sphere(0.1f, app);  
  120.        objTrans4 = new TransformGroup();
  121.        objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  122.        Transform3D kulka2 = new Transform3D();
  123.        kulka2.setTranslation(new Vector3f(0.0f, 0.0f, 1.0f));
  124.        objTrans4.setTransform(kulka2);
  125.        objTrans4.addChild(przedmiot);
  126.        scena.addChild(objTrans4);
  127.        
  128.        // podłoże
  129.        Appearance ap = new Appearance();
  130.        Color3f black = new Color3f(.0f,.0f,.0f);
  131.        Color3f white = new Color3f(0.8f,.8f,.8f);
  132.        ap.setMaterial(new Material(white,black,white,black,80f));
  133.        podloga ob = new podloga();
  134.        ob.setAppearance(ap);
  135.        scena.addChild(ob);
  136.        
  137.        // światło kierunkowe
  138.        Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
  139.        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
  140.        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
  141.        light1.setInfluencingBounds(bounds);
  142.        scena.addChild(light1);
  143.        
  144.        // światło
  145.        Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
  146.        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  147.        ambientLightNode.setInfluencingBounds(bounds);
  148.        scena.addChild(ambientLightNode);
  149.        
  150.        //objTrans2.addChild(objTrans3);
  151.        objTrans.addChild(objTrans2);
  152.        scena.addChild(objTrans);
  153.      
  154.     // Create a new Behavior object that will perform the collision
  155.     // detection on the specified object, and add it into
  156.     // the scene graph.
  157.     CollisionDetector cd = new CollisionDetector(przedmiot);
  158.     BoundingSphere boundy = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  159.         1.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 Sphere sferka;
  216.  
  217.   private ColoringAttributes shapeColoring;
  218.  
  219.   private Appearance shapeAppearance;
  220.  
  221.   private WakeupOnCollisionEntry wEnter;
  222.  
  223.   private WakeupOnCollisionExit wExit;
  224.  
  225.   public CollisionDetector(Sphere s) {
  226.     sferka = s;
  227.     shapeAppearance = sferka.getAppearance();
  228.     shapeColoring = shapeAppearance.getColoringAttributes();
  229.     inCollision = false;
  230.   }
  231.  
  232.   public void initialize() {
  233.     wEnter = new WakeupOnCollisionEntry(sferka);
  234.     wExit = new WakeupOnCollisionExit(sferka);
  235.     wakeupOn(wEnter);
  236.   }
  237.  
  238.   public void processStimulus(Enumeration criteria) {
  239.     inCollision = !inCollision;
  240.  
  241.     if (inCollision) {
  242.       shapeAppearance.setColoringAttributes(highlight);
  243.       wakeupOn(wExit);
  244.     } else {
  245.       shapeAppearance.setColoringAttributes(shapeColoring);
  246.       wakeupOn(wEnter);
  247.     }
  248.   }
  249.   }
  250.  
  251. /******************************************************************************/    
  252.    
  253.    public static float round (double f)
  254.  
  255.    {  float temp = (float)(f*(Math.pow(10, 0)));
  256.  
  257.           temp = (Math.round(temp));
  258.  
  259.           temp = temp/(int)(Math.pow(10, 0));
  260.  
  261.           return temp;
  262.  
  263.    }
  264.     /**
  265.      * Funkcja wychwytuje zdarzenie wciśniecią przycisku na klawiaturze.
  266.      * Zawiera obsługę przycisków służących do manipulacji robotem.
  267.      * UP, DOWN     - przesuw ramienia w pionie
  268.      * LEFT, RIGHT  - obrót ramienia wokół jego osi pionowej
  269.      * A, S         - przesuw ramienia w poziomie (wysuwanie, wsuwanie)
  270.      * Z            - standardowe ustawienie kamery
  271.      * @param e (KeyEvent)
  272.      */
  273.     @Override
  274.     public void keyPressed(KeyEvent e) {
  275.         if(e.getKeyCode() == KeyEvent.VK_Z){
  276.             vPlatform.setNominalViewingTransform();
  277.         }
  278.         if (Math.round(Math.toDegrees(x))==360) x=Math.toRadians(0);
  279.         if (Math.toDegrees(x)==-1) x=Math.toRadians(358.9999999999974);
  280.         switch(e.getKeyCode()){
  281.             case KeyEvent.VK_UP:
  282.                 if(y < 0.65f) {
  283.                     y += 0.05;
  284.                     y2 += 0.05;
  285.                 }
  286.                 break;
  287.             case KeyEvent.VK_DOWN:
  288.                 if(y > -0.6f) {
  289.                     y -= 0.05;
  290.                     y2 -= 0.05;
  291.                 }
  292.                 break;
  293.             case KeyEvent.VK_LEFT:
  294.                 x -= Math.PI/180;
  295.                 break;
  296.             case KeyEvent.VK_RIGHT:
  297.                 x += Math.PI/180;
  298.                 break;
  299.             case KeyEvent.VK_A:
  300.                 if(z > -0.3f) {
  301.                     z -= 0.05;
  302.                     z2 -= 0.05;
  303.                 }
  304.                 break;
  305.             case KeyEvent.VK_S:
  306.                 if(z < 0.5f) {
  307.                     z += 0.05;
  308.                     z2 += 0.05;
  309.                 }
  310.                 break;
  311.             case KeyEvent.VK_SPACE:
  312.                 spacja = !spacja;
  313.                 zatrzask = true;
  314.         }
  315.     }
  316.  
  317.     /**
  318.      * Funkcja wychwytująca puszczenie przycisku klawiatury - nie używana.
  319.      * @param e
  320.      */
  321.     @Override
  322.     public void keyReleased(KeyEvent e){
  323.         switch(e.getKeyCode()){
  324.             case KeyEvent.VK_SPACE:
  325.                 zatrzask = false;
  326.                 break;
  327.         }
  328.     }
  329.  
  330.     /**
  331.      * Funkcja wychwytująca sygnał przycisku klawiatury - nie używana.
  332.      * @param e
  333.      */
  334.     @Override
  335.     public void keyTyped(KeyEvent e){
  336.     }
  337.    
  338.  
  339. //    @Override
  340. //    public void mouseMoved(MouseEvent e) {
  341. //       // x = (float)(e.getX())/Width-.5f;
  342. //        //y = -(float)(e.getY())/Height+.5f;
  343. //    }
  344.    
  345.     /**
  346.      * Klasa dziedzicząca po klasie TimerTask.
  347.      * Zawiera funkcję run().
  348.      */
  349.     private class Ruch extends TimerTask{
  350.         /**
  351.          * Zadaniem funkcji jest przekształcanie
  352.          * sceny w zależności od parametrów (zmienne globalne) oraz wyświetlanie
  353.          * informacji o aktualnych wartościach parametrów w dolnym pasku
  354.          * informacyjnym.
  355.          */
  356.         @Override
  357.         public void run() {            
  358.             trans.rotX(Math.PI/2);
  359.             trans.setTranslation(new Vector3d(0.0f, y, z));
  360.             trans2.setTranslation(new Vector3d(0.0f,y2,z2));
  361.             trans3.setTranslation(new Vector3d(0.0f, 0.0f, 1.0f));
  362.             objTrans2.setTransform(trans);
  363.             hujRotation.rotY(x);
  364.             trans.mul(hujRotation, trans);
  365.             trans2.mul(hujRotation, trans2);
  366.             trans3.mul(hujRotation, trans3);
  367.             objTrans2.setTransform(trans);
  368.             objTrans3.setTransform(trans2);
  369.             if (spacja==true && inCollision==true) objTrans4.setTransform(trans3);
  370.             label.setText("Współrzędne: " + Math.round(Math.toDegrees(x)) + " , "+ y + " , " + z);
  371.         }
  372.     }
  373.  
  374.     /**
  375.      * Klasa 'podloga' dziedzicząca po klasie 'Shape3D'.
  376.      */
  377.     public class podloga extends Shape3D{ // podłoże robota
  378.         final private Point3f A= new Point3f(-5.0f, -0.5f, -5.0f);
  379.         final private Point3f B= new Point3f(-5.0f, -0.5f, 5.0f);
  380.         final private Point3f C= new Point3f(5.0f, -0.5f, 5.0f);
  381.         final private Point3f D= new Point3f(5.0f, -0.5f, -5.0f);
  382.         final private Point3f[] pts = new Point3f[8];
  383.         int[] stripCounts= new int[2];
  384.         int[] contourCount=new int[2];
  385.        
  386.         /**
  387.          * Konstruktor bezparametrowy klasy 'podloga'. Tworzy punkt odniesienia
  388.          * dla manipulatora.
  389.          */
  390.         public podloga(){
  391.             // front
  392.             pts[0]=C;
  393.             pts[1]=D;
  394.             pts[2]=A;
  395.             pts[3]=B;
  396.             //back
  397.             pts[4]=C;
  398.             pts[5]=B;
  399.             pts[6]=A;
  400.             pts[7]=D;
  401.            
  402.             stripCounts[0]=4;
  403.             stripCounts[1]=4;
  404.             contourCount[0]=1;
  405.             contourCount[1]=1;
  406.             GeometryInfo gInf = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
  407.             gInf.setCoordinates(pts);
  408.             gInf.setStripCounts(stripCounts);
  409.             gInf.setContourCounts(contourCount);
  410.             NormalGenerator ng= new NormalGenerator();
  411.             ng.setCreaseAngle ((float) Math.toRadians(30));
  412.             ng.generateNormals(gInf);
  413.             this.setGeometry(gInf.getGeometryArray());
  414.             }
  415.     }
  416.    
  417. }
Advertisement
Add Comment
Please, Sign In to add comment