home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / dialogs / TransformDialog.bak < prev    next >
Text File  |  1997-01-05  |  9KB  |  321 lines

  1. // VRML Generator
  2. // (c) Justin Couch
  3. //
  4. // From Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // The Transform node dialog.
  7.  
  8. package ui.dialogs;
  9.  
  10. import java.awt.*;
  11. import ui.dialogs.VrmlBaseDialog;
  12. import geometry.Transform;
  13. import ui.VrmlTree;
  14. import VrmlScene;
  15. import VrmlTypes;
  16. import exceptions.NoSelectedNodeException;
  17.  
  18.  
  19. public class TransformDialog extends VrmlBaseDialog
  20. {
  21.  
  22.     private TextField    trans_x, trans_y, trans_z;
  23.     private TextField    center_x, center_y, center_z;
  24.     private TextField    rot_x, rot_y, rot_z, rot_a;
  25.     private TextField    scale_x, scale_y, scale_z;
  26.     private TextField    scaleO_x, scaleO_y, scaleO_z, scaleO_a;
  27.     private TextField    bbcenter_x, bbcenter_y, bbcenter_z;
  28.     private TextField    bbsize_x, bbsize_y, bbsize_z;
  29.  
  30.     public TransformDialog(Frame parent, VrmlTree tree, VrmlScene data)
  31.     {
  32.         super(parent, "Edit Transform Node");
  33.  
  34.         vrml_data = data;
  35.         screen_data = tree;
  36.  
  37.         content_panel.setLayout(new GridLayout(8, 1));
  38.  
  39.         // translation fields
  40.         Panel p1 = new Panel();
  41.         p1.setLayout(new BorderLayout(5, 0));
  42.  
  43.         p1.add("West", new Label("Translation", Label.LEFT));
  44.  
  45.         Panel p2 = new Panel();
  46.         p2.setLayout(new GridLayout(1, 8));
  47.  
  48.         // the 3 components.
  49.         trans_x = new TextField("0", 5);
  50.         trans_y = new TextField("0", 5);
  51.         trans_z = new TextField("0", 5);
  52.  
  53.         p2.add(new Label("x: ", Label.RIGHT));
  54.         p2.add(trans_x);
  55.         p2.add(new Label("y: ", Label.RIGHT));
  56.         p2.add(trans_y);
  57.         p2.add(new Label("z: ", Label.RIGHT));
  58.         p2.add(trans_z);
  59.  
  60.         p1.add("East", p2);
  61.         content_panel.add(p1);
  62.  
  63.         // center fields
  64.         Panel p3 = new Panel();
  65.         p3.setLayout(new BorderLayout(15, 10));
  66.  
  67.         p3.add("West", new Label("Center", Label.LEFT));
  68.  
  69.         Panel p4 = new Panel();
  70.         p4.setLayout(new GridLayout(1, 8));
  71.  
  72.         // the 3 components.
  73.         center_x = new TextField("0", 5);
  74.         center_y = new TextField("0", 5);
  75.         center_z = new TextField("0", 5);
  76.  
  77.         p4.add(new Label("x: ", Label.RIGHT));
  78.         p4.add(center_x);
  79.         p4.add(new Label("y: ", Label.RIGHT));
  80.         p4.add(center_y);
  81.         p4.add(new Label("z: ", Label.RIGHT));
  82.         p4.add(center_z);
  83.  
  84.         p3.add("East", p4);
  85.         content_panel.add(p3);
  86.  
  87.         // rotation fields
  88.         Panel p5 = new Panel();
  89.         p5.setLayout(new BorderLayout(5, 0));
  90.  
  91.         p5.add("West", new Label("Rotation", Label.LEFT));
  92.  
  93.         Panel p6 = new Panel();
  94.         p6.setLayout(new GridLayout(1, 8));
  95.  
  96.         // the 4 components.
  97.         rot_x = new TextField("0", 5);
  98.         rot_y = new TextField("0", 5);
  99.         rot_z = new TextField("1", 5);
  100.         rot_a = new TextField("0", 5);
  101.  
  102.         p6.add(new Label("x: ", Label.RIGHT));
  103.         p6.add(rot_x);
  104.         p6.add(new Label("y: ", Label.RIGHT));
  105.         p6.add(rot_y);
  106.         p6.add(new Label("z: ", Label.RIGHT));
  107.         p6.add(rot_z);
  108.         p6.add(new Label("angle: ", Label.RIGHT));
  109.         p6.add(rot_a);
  110.  
  111.         p5.add("East", p6);
  112.         content_panel.add(p5);
  113.  
  114.         // scale fields
  115.         Panel p7 = new Panel();
  116.         p7.setLayout(new BorderLayout(5, 0));
  117.  
  118.         p7.add("West", new Label("Scale", Label.LEFT));
  119.  
  120.         Panel p8 = new Panel();
  121.         p8.setLayout(new GridLayout(1, 8));
  122.  
  123.         // the 3 components.
  124.         scale_x = new TextField("1", 5);
  125.         scale_y = new TextField("1", 5);
  126.         scale_z = new TextField("1", 5);
  127.  
  128.         p8.add(new Label("x: ", Label.RIGHT));
  129.         p8.add(scale_x);
  130.         p8.add(new Label("y: ", Label.RIGHT));
  131.         p8.add(scale_y);
  132.         p8.add(new Label("z: ", Label.RIGHT));
  133.         p8.add(scale_z);
  134.  
  135.         p7.add("East", p8);
  136.         content_panel.add(p7);
  137.  
  138.         // scale Orientation
  139.         Panel p9 = new Panel();
  140.         p9.setLayout(new BorderLayout(5, 0));
  141.  
  142.         p9.add("West", new Label("ScaleOrientation"));
  143.  
  144.         Panel p10 = new Panel();
  145.         p10.setLayout(new GridLayout(1, 8));
  146.  
  147.         // the 3 components.
  148.         scaleO_x = new TextField("0", 5);
  149.         scaleO_y = new TextField("0", 5);
  150.         scaleO_z = new TextField("1", 5);
  151.         scaleO_a = new TextField("0", 5);
  152.  
  153.         p10.add(new Label("x: ", Label.RIGHT));
  154.         p10.add(scaleO_x);
  155.         p10.add(new Label("y: ", Label.RIGHT));
  156.         p10.add(scaleO_y);
  157.         p10.add(new Label("z: ", Label.RIGHT));
  158.         p10.add(scaleO_z);
  159.         p10.add(new Label("angle: ", Label.RIGHT));
  160.         p10.add(scaleO_a);
  161.  
  162.         p9.add("East", p10);
  163.         content_panel.add(p9);
  164.  
  165.         // Bounding Box Stuff
  166.         content_panel.add(new Label("Bounding Box"));
  167.  
  168.         // now the first row. On the left is the Label and on the right is
  169.         // the 3 boxes for x, y and z, indicated by appropriate labels.
  170.         Panel p11 = new Panel();
  171.         p11.setLayout(new BorderLayout(5, 0));
  172.  
  173.         p11.add("West", new Label("Center"));
  174.  
  175.         Panel p12 = new Panel();
  176.         p12.setLayout(new GridLayout(1, 8));
  177.  
  178.         // the 3 components.
  179.         bbcenter_x = new TextField("0", 5);
  180.         bbcenter_y = new TextField("0", 5);
  181.         bbcenter_z = new TextField("0", 5);
  182.  
  183.         p12.add(new Label("x: ", Label.RIGHT));
  184.         p12.add(bbcenter_x);
  185.         p12.add(new Label("y: ", Label.RIGHT));
  186.         p12.add(bbcenter_y);
  187.         p12.add(new Label("z: ", Label.RIGHT));
  188.         p12.add(bbcenter_z);
  189.  
  190.         p11.add("East", p12);
  191.         content_panel.add(p11);
  192.  
  193.         // The second row. Identical to the first with the exception that
  194.         // we have changed a label over
  195.         Panel p13 = new Panel();
  196.         p13.setLayout(new BorderLayout(5, 0));
  197.  
  198.         p13.add("West", new Label("Size"));
  199.  
  200.         Panel p14 = new Panel();
  201.         p14.setLayout(new GridLayout(1, 8));
  202.  
  203.         // the 3 components.
  204.         bbsize_x = new TextField("-1", 5);
  205.         bbsize_y = new TextField("-1", 5);
  206.         bbsize_z = new TextField("-1", 5);
  207.  
  208.         p14.add(new Label("x: ", Label.RIGHT));
  209.         p14.add(bbsize_x);
  210.         p14.add(new Label("y: ", Label.RIGHT));
  211.         p14.add(bbsize_y);
  212.         p14.add(new Label("z: ", Label.RIGHT));
  213.         p14.add(bbsize_z);
  214.  
  215.         p13.add("East", p14);
  216.         content_panel.add(p13);
  217.  
  218.         pack();
  219.     }
  220.  
  221.     // read all the information back out of the panel and pass it
  222.     // to the appropriate place.
  223.     public boolean applyInformation()
  224.     {
  225.         String str;
  226.  
  227.         float[]    bbcenter = new float[3];
  228.         float[] bbsize = new float[3];
  229.         float[] tx = new float[3];
  230.         float[] center = new float[3];
  231.         float[] rot = new float[4];
  232.         float[] scale = new float[3];
  233.         float[] scaleO = new float[4];
  234.  
  235.         Transform transform;
  236.  
  237.         // first get the basic node constructed
  238.         bbcenter[0] = Float.valueOf(bbcenter_x.getText()).floatValue();
  239.         bbcenter[1] = Float.valueOf(bbcenter_y.getText()).floatValue();
  240.         bbcenter[2] = Float.valueOf(bbcenter_z.getText()).floatValue();
  241.  
  242.         bbsize[0] = Float.valueOf(bbsize_x.getText()).floatValue();
  243.         bbsize[1] = Float.valueOf(bbsize_y.getText()).floatValue();
  244.         bbsize[2] = Float.valueOf(bbsize_z.getText()).floatValue();
  245.  
  246.         // are we creating a default node? This test for a yes
  247.         if((bbcenter[0] != 0) || (bbcenter[1] != 0) || (bbcenter[2] != 0) ||
  248.            (bbsize[0] != -1) || (bbsize[1] != -1) || (bbsize[2] != -1))
  249.             transform = new Transform(browser, bbcenter, bbsize);
  250.         else
  251.             transform = new Transform(browser);
  252.  
  253.         System.out.println("Node created");
  254.  
  255.         str = name.getText();
  256.  
  257.         if(str.equals(""))
  258.             transform.name = null;
  259.         else
  260.             transform.name = new String();
  261.  
  262.         // now we update the fields
  263.         // translation
  264.         tx[0] = Float.valueOf(trans_x.getText()).floatValue();
  265.         tx[1] = Float.valueOf(trans_y.getText()).floatValue();
  266.         tx[2] = Float.valueOf(trans_z.getText()).floatValue();
  267.  
  268.         if((tx[0] != 0) || (tx[1] != 0) || (tx[2] != 0))
  269.             transform.set_translation(tx);
  270.  
  271.         // center
  272.         center[0] = Float.valueOf(center_x.getText()).floatValue();
  273.         center[1] = Float.valueOf(center_y.getText()).floatValue();
  274.         center[2] = Float.valueOf(center_z.getText()).floatValue();
  275.  
  276.         if((center[0] != 0) || (center[1] != 0) || (center[2] != 0))
  277.             transform.set_center(center);
  278.  
  279.         // rotation
  280.         rot[0] = Float.valueOf(rot_x.getText()).floatValue();
  281.         rot[1] = Float.valueOf(rot_y.getText()).floatValue();
  282.         rot[2] = Float.valueOf(rot_z.getText()).floatValue();
  283.         rot[3] = Float.valueOf(rot_a.getText()).floatValue();
  284.  
  285.         if((rot[0] != 0) || (rot[1] != 0) || (rot[2] != 1) || (rot[3] != 0))
  286.             transform.set_rotation(rot);
  287.  
  288.         // scale
  289.         scale[0] = Float.valueOf(scale_x.getText()).floatValue();
  290.         scale[1] = Float.valueOf(scale_y.getText()).floatValue();
  291.         scale[2] = Float.valueOf(scale_z.getText()).floatValue();
  292.  
  293.         if((scale[0] != 1) || (scale[1] != 1) || (scale[2] != 1))
  294.             transform.set_scale(scale);
  295.  
  296.         // scaleOrientation
  297.         scaleO[0] = Float.valueOf(scaleO_x.getText()).floatValue();
  298.         scaleO[1] = Float.valueOf(scaleO_y.getText()).floatValue();
  299.         scaleO[2] = Float.valueOf(scaleO_z.getText()).floatValue();
  300.         scaleO[3] = Float.valueOf(scaleO_a.getText()).floatValue();
  301.  
  302.         if((scaleO[0] != 0) || (scaleO[1] != 0) ||
  303.            (scaleO[2] != 1) || (scaleO[3] != 0))
  304.             transform.set_scaleOrientation(scaleO);
  305.  
  306.  
  307.         try
  308.         {
  309.             System.out.println("About to add");
  310.             vrml_data.addNode(VrmlTypes.Transform, transform);
  311.             System.out.println("done adding");
  312.         }
  313.         catch (NoSelectedNodeException e)
  314.         {
  315.             System.out.println("No node was selected");
  316.         }
  317.  
  318.         return false;
  319.     }
  320.  
  321. }