jotto

Untitled

Jun 10th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.07 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 x2 = 0.1f;
  38.     private double y2 = 0.9f;
  39.     private double z2 = 1.1f;
  40.     JLabel label = new JLabel("Ramię Robota:");
  41.     private TransformGroup objTrans;
  42.     final private Transform3D trans = new Transform3D();
  43.     private TransformGroup objTrans2;
  44.     final private Transform3D trans2 = new Transform3D();
  45.     private TransformGroup objTrans3;
  46.     final private Transform3D trans3 = new Transform3D();
  47.     private TransformGroup objTrans4;
  48.     final private Transform3D trans4 = new Transform3D();
  49.     private RotationInterpolator rotacja;
  50.     final private Transform3D hujRotation = new Transform3D();
  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.        Transform3D cyl2 = new Transform3D();
  90.        tmp.set(new Vector3f(0.0f,0.0f,0.0f));
  91.        cyl2.rotX(Math.PI/2);
  92.        tmp.mul(cyl2);
  93.        cyl2.setTranslation(new Vector3d(x, y, z));
  94.        objTrans2.setTransform(cyl2);
  95.        objTrans2.addChild(cylinder2);
  96.        //scena.addChild(objTrans2);
  97.        
  98.        //kulka
  99.        scena.addChild(objTrans3);
  100.        Sphere chwytak = new Sphere(0.08f);
  101.        objTrans3 = new TransformGroup();
  102.        objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  103.        Transform3D kulka = new Transform3D();
  104.        kulka.setTranslation(new Vector3d(x2, y2, z2));
  105.        objTrans3.setTransform(kulka);
  106.        objTrans3.addChild(chwytak);
  107.        scena.addChild(objTrans3);
  108.        
  109.        //kulka
  110.        scena.addChild(objTrans4);
  111.        
  112.        Appearance app = new Appearance();
  113.        app.setColoringAttributes(new ColoringAttributes(1.0f, 0.0f, 0.0f,ColoringAttributes.ALLOW_COLOR_WRITE));
  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 black = new Color3f(.0f,.0f,.0f);
  127.        Color3f white = new Color3f(0.8f,.8f,.8f);
  128.        ap.setMaterial(new Material(white,black,white,black,80f));
  129.        podloga ob = new podloga();
  130.        ob.setAppearance(ap);
  131.        scena.addChild(ob);
  132.        
  133.        // światło kierunkowe
  134.        Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
  135.        Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
  136.        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
  137.        light1.setInfluencingBounds(bounds);
  138.        scena.addChild(light1);
  139.        
  140.        // światło
  141.        Color3f ambientColor = new Color3f(1.0f, 1.0f, 1.0f);
  142.        AmbientLight ambientLightNode = new AmbientLight(ambientColor);
  143.        ambientLightNode.setInfluencingBounds(bounds);
  144.        scena.addChild(ambientLightNode);
  145.        
  146.        //objTrans2.addChild(objTrans3);
  147.        objTrans.addChild(objTrans2);
  148.        scena.addChild(objTrans);
  149.      
  150.     // Create a new Behavior object that will perform the collision
  151.     // detection on the specified object, and add it into
  152.     // the scene graph.
  153.     CollisionDetector cd = new CollisionDetector(przedmiot);
  154.     BoundingSphere boundy = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
  155.         50.0);
  156.     cd.setSchedulingBounds(boundy);      
  157.        objTrans3.addChild(cd);
  158.        
  159.        scena.compile();
  160.        return scena;
  161.     }
  162.    
  163.  
  164.     /**
  165.      * Konstruktor bezparametrowy klasy 'Main'. Ustawia on wygląd okna programu
  166.      * (BorderLayout()), tworzy w nim podstawowe obiekty Java3D (SimpleUniverse).
  167.      * Ustawia pozycję i zachowanie kamery oraz uruchamia zegar.
  168.      */
  169.     public Main() {
  170.         setLayout(new BorderLayout());
  171.        
  172.         GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
  173.         Canvas3D canvas3D = new Canvas3D(config);
  174.         add("Center", canvas3D);
  175.         canvas3D.addKeyListener(this);
  176.         add("South",label);
  177.        
  178.         // Create a simple scene and attach it to the virtual universe
  179.         BranchGroup scene = createSceneGraph();
  180.         universe = new SimpleUniverse(canvas3D);
  181.         universe.addBranchGraph(scene);
  182.         obserwator = new OrbitBehavior(canvas3D);
  183.         BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  184.         obserwator.setSchedulingBounds(bounds);
  185.         vPlatform = universe.getViewingPlatform();
  186.         Transform3D temp = new Transform3D();
  187.         temp.set(new Vector3f(0f,0f,7.0f));
  188.         vPlatform.getViewPlatformTransform().setTransform(temp);
  189.         vPlatform.setViewPlatformBehavior(obserwator);
  190.         zegar.scheduleAtFixedRate(new Ruch(), 20, 25);
  191.     }
  192.  
  193.     /**
  194.      * Funkcja tworzy okno typu MainFrame o zadanych wymiarach Width i Height.
  195.      * Konstruktor okna wywołuje konstruktor klasy Main().
  196.      * @param args
  197.      */
  198.     public static void main(String[] args) {
  199.        //Main bb = new Main();  
  200.        MainFrame mf = new MainFrame(new Main(),Width, Height);
  201.     }
  202.    
  203. /******************************************************************************/    
  204.    
  205.   class CollisionDetector extends Behavior {
  206.   private final Color3f highlightColor = new Color3f(0.0f, 1.0f, 0.0f);
  207.  
  208.   private final ColoringAttributes highlight = new ColoringAttributes(
  209.       highlightColor, ColoringAttributes.SHADE_GOURAUD);
  210.  
  211.   private boolean inCollision = false;
  212.  
  213.   private Sphere sferka;
  214.  
  215.   private ColoringAttributes shapeColoring;
  216.  
  217.   private Appearance shapeAppearance;
  218.  
  219.   private WakeupOnCollisionEntry wEnter;
  220.  
  221.   private WakeupOnCollisionExit wExit;
  222.  
  223.   public CollisionDetector(Sphere s) {
  224.     sferka = s;
  225.     shapeAppearance = sferka.getAppearance();
  226.     shapeColoring = shapeAppearance.getColoringAttributes();
  227.     inCollision = false;
  228.   }
  229.  
  230.   @Override
  231.   public void initialize() {
  232.     wEnter = new WakeupOnCollisionEntry(sferka);
  233.     wExit = new WakeupOnCollisionExit(sferka);
  234.     wakeupOn(wEnter);
  235.   }
  236.  
  237.   @Override
  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.         }
  312.     }
  313.  
  314.     /**
  315.      * Funkcja wychwytująca puszczenie przycisku klawiatury - nie używana.
  316.      * @param e
  317.      */
  318.     @Override
  319.     public void keyReleased(KeyEvent e){
  320.     }
  321.  
  322.     /**
  323.      * Funkcja wychwytująca sygnał przycisku klawiatury - nie używana.
  324.      * @param e
  325.      */
  326.     @Override
  327.     public void keyTyped(KeyEvent e){
  328.     }
  329.    
  330.  
  331. //    @Override
  332. //    public void mouseMoved(MouseEvent e) {
  333. //       // x = (float)(e.getX())/Width-.5f;
  334. //        //y = -(float)(e.getY())/Height+.5f;
  335. //    }
  336.    
  337.     /**
  338.      * Klasa dziedzicząca po klasie TimerTask.
  339.      * Zawiera funkcję run().
  340.      */
  341.     private class Ruch extends TimerTask{
  342.         /**
  343.          * Zadaniem funkcji jest przekształcanie
  344.          * sceny w zależności od parametrów (zmienne globalne) oraz wyświetlanie
  345.          * informacji o aktualnych wartościach parametrów w dolnym pasku
  346.          * informacyjnym.
  347.          */
  348.         @Override
  349.         public void run() {            
  350.             trans.rotX(Math.PI/2);
  351.             trans.setTranslation(new Vector3d(0.1f, y, z));
  352.             trans2.setTranslation(new Vector3d(0.1f,y2,z2));
  353.             objTrans2.setTransform(trans);
  354.  
  355.             hujRotation.rotY(x);
  356.             trans.mul(hujRotation, trans);
  357.             trans2.mul(hujRotation, trans2);
  358.             objTrans2.setTransform(trans);
  359.             objTrans3.setTransform(trans2);
  360.             label.setText("Współrzędne: " + Math.round(Math.toDegrees(x)) + " , "+ y + " , " + z);
  361.         }
  362.     }
  363.  
  364.     /**
  365.      * Klasa 'podloga' dziedzicząca po klasie 'Shape3D'.
  366.      */
  367.     public class podloga extends Shape3D{ // podłoże robota
  368.         final private Point3f A= new Point3f(-5.0f, -0.5f, -5.0f);
  369.         final private Point3f B= new Point3f(-5.0f, -0.5f, 5.0f);
  370.         final private Point3f C= new Point3f(5.0f, -0.5f, 5.0f);
  371.         final private Point3f D= new Point3f(5.0f, -0.5f, -5.0f);
  372.         final private Point3f[] pts = new Point3f[8];
  373.         int[] stripCounts= new int[2];
  374.         int[] contourCount=new int[2];
  375.        
  376.         /**
  377.          * Konstruktor bezparametrowy klasy 'podloga'. Tworzy punkt odniesienia
  378.          * dla manipulatora.
  379.          */
  380.         public podloga(){
  381.             // front
  382.             pts[0]=C;
  383.             pts[1]=D;
  384.             pts[2]=A;
  385.             pts[3]=B;
  386.             //back
  387.             pts[4]=C;
  388.             pts[5]=B;
  389.             pts[6]=A;
  390.             pts[7]=D;
  391.            
  392.             stripCounts[0]=4;
  393.             stripCounts[1]=4;
  394.             contourCount[0]=1;
  395.             contourCount[1]=1;
  396.             GeometryInfo gInf = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
  397.             gInf.setCoordinates(pts);
  398.             gInf.setStripCounts(stripCounts);
  399.             gInf.setContourCounts(contourCount);
  400.             NormalGenerator ng= new NormalGenerator();
  401.             ng.setCreaseAngle ((float) Math.toRadians(30));
  402.             ng.generateNormals(gInf);
  403.             this.setGeometry(gInf.getGeometryArray());
  404.             }
  405.     }
  406.    
  407. }
Advertisement
Add Comment
Please, Sign In to add comment