Advertisement
jotto

Untitled

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