Advertisement
jotto

Untitled

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