jotto

Untitled

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