home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / ui / dialogs / MaterialDialog.java < prev    next >
Text File  |  1997-01-02  |  5KB  |  219 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.Material;
  13. import ui.VrmlTree;
  14. import VrmlScene;
  15. import VrmlTypes;
  16. import exceptions.NoSelectedNodeException;
  17.  
  18.  
  19. public class MaterialDialog extends VrmlBaseDialog
  20. {
  21.     private TextField    ambient;
  22.     private TextField    diffuse_x, diffuse_y, diffuse_z;
  23.     private TextField    emissive_x, emissive_y, emissive_z;
  24.     private TextField    shininess;
  25.     private TextField    spec_x, spec_y, spec_z;
  26.     private TextField    transparency;
  27.  
  28.     public MaterialDialog(Frame parent, VrmlTree tree, VrmlScene data)
  29.     {
  30.         super(parent, "Edit Group Node");
  31.  
  32.         vrml_data = data;
  33.         screen_data = tree;
  34.  
  35.         content_panel.setLayout(new GridLayout(8, 1));
  36.  
  37.         // translation fields
  38.         Panel p1 = new Panel();
  39.         p1.setLayout(new BorderLayout(15, 10));
  40.  
  41.         p1.add("West", new Label("Ambient Intensity", Label.LEFT));
  42.  
  43.         ambient = new TextField("0.2", 4);
  44.  
  45.         p1.add("East", ambient);
  46.         content_panel.add(p1);
  47.  
  48.         // diffuse Colour
  49.         Panel p3 = new Panel();
  50.         p3.setLayout(new BorderLayout(15, 10));
  51.  
  52.         p3.add("West", new Label("Diffuse Colour", Label.LEFT));
  53.  
  54.         Panel p4 = new Panel();
  55.         p4.setLayout(new GridLayout(1, 6));
  56.  
  57.         // the 3 components.
  58.         diffuse_x = new TextField("0.8", 4);
  59.         diffuse_y = new TextField("0.8", 4);
  60.         diffuse_z = new TextField("0.8", 4);
  61.  
  62.         p4.add(new Label("R: ", Label.RIGHT));
  63.         p4.add(diffuse_x);
  64.         p4.add(new Label("G: ", Label.RIGHT));
  65.         p4.add(diffuse_y);
  66.         p4.add(new Label("B: ", Label.RIGHT));
  67.         p4.add(diffuse_z);
  68.  
  69.         p3.add("East", p4);
  70.         content_panel.add(p3);
  71.  
  72.         // Emissive Colour
  73.         Panel p5 = new Panel();
  74.         p5.setLayout(new BorderLayout(15, 10));
  75.  
  76.         p5.add("West", new Label("Emissive Colour", Label.LEFT));
  77.  
  78.         Panel p6 = new Panel();
  79.         p6.setLayout(new GridLayout(1, 6));
  80.  
  81.         // the 4 components.
  82.         emissive_x = new TextField("0", 4);
  83.         emissive_y = new TextField("0", 4);
  84.         emissive_z = new TextField("0", 4);
  85.  
  86.         p6.add(new Label("R: ", Label.RIGHT));
  87.         p6.add(emissive_x);
  88.         p6.add(new Label("G: ", Label.RIGHT));
  89.         p6.add(emissive_y);
  90.         p6.add(new Label("B: ", Label.RIGHT));
  91.         p6.add(emissive_z);
  92.  
  93.         p5.add("East", p6);
  94.         content_panel.add(p5);
  95.  
  96.         // specular Colour
  97.         Panel p7 = new Panel();
  98.         p7.setLayout(new BorderLayout(15, 10));
  99.  
  100.         p7.add("West", new Label("Specular Colour", Label.LEFT));
  101.  
  102.         Panel p8 = new Panel();
  103.         p8.setLayout(new GridLayout(1, 6));
  104.  
  105.         // the 3 components.
  106.         spec_x = new TextField("0", 4);
  107.         spec_y = new TextField("0", 4);
  108.         spec_z = new TextField("0", 4);
  109.  
  110.         p8.add(new Label("R: ", Label.RIGHT));
  111.         p8.add(spec_x);
  112.         p8.add(new Label("G: ", Label.RIGHT));
  113.         p8.add(spec_y);
  114.         p8.add(new Label("B: ", Label.RIGHT));
  115.         p8.add(spec_z);
  116.  
  117.         p7.add("East", p8);
  118.         content_panel.add(p7);
  119.  
  120.         // Shininess
  121.         Panel p9 = new Panel();
  122.         p9.setLayout(new BorderLayout(15, 10));
  123.  
  124.         p9.add("West", new Label("Shininess", Label.LEFT));
  125.  
  126.         shininess = new TextField("0.2", 4);
  127.  
  128.         p9.add("East", shininess);
  129.         content_panel.add(p9);
  130.  
  131.         // Transparency
  132.         Panel p10 = new Panel();
  133.         p10.setLayout(new BorderLayout(15, 10));
  134.  
  135.         p10.add("West", new Label("Transparency", Label.LEFT));
  136.  
  137.         transparency = new TextField("0", 4);
  138.  
  139.         p10.add("East", transparency);
  140.         content_panel.add(p10);
  141.  
  142.         pack();
  143.     }
  144.  
  145.     // read all the information back out of the panel and pass it
  146.     // to the appropriate place.
  147.     public boolean applyInformation()
  148.     {
  149.         String str;
  150.  
  151.         float    diff_r, diff_g, diff_b;
  152.         float    emis_r, emis_g, emis_b;
  153.         float    spec_r, spec_g, spec_b;
  154.         float    amb;
  155.         float    shine;
  156.         float    trans;
  157.  
  158.         Material material;
  159.  
  160.         // first get the basic node constructed
  161.         material = new Material(browser);
  162.  
  163.         str = name.getText();
  164.  
  165.         if(str.equals(""))
  166.             material.name = null;
  167.         else
  168.             material.name = new String(str);
  169.  
  170.         // now we update the fields
  171.         // ambient intensity
  172.         amb = Float.valueOf(ambient.getText()).floatValue();
  173.         if(amb != 0.2f)
  174.             material.set_ambientIntensity(amb);
  175.  
  176.         // diffuseColor
  177.         diff_r = Float.valueOf(diffuse_x.getText()).floatValue();
  178.         diff_g = Float.valueOf(diffuse_y.getText()).floatValue();
  179.         diff_b = Float.valueOf(diffuse_z.getText()).floatValue();
  180.         if((diff_r != 0.8f) || (diff_g != 0.8f) || (diff_b != 0.8f))
  181.             material.set_diffuseColor(diff_r, diff_g, diff_b);
  182.  
  183.         // emissiveColor
  184.         emis_r = Float.valueOf(emissive_x.getText()).floatValue();
  185.         emis_g = Float.valueOf(emissive_y.getText()).floatValue();
  186.         emis_b = Float.valueOf(emissive_z.getText()).floatValue();
  187.         if((emis_r != 0) || (emis_g != 0) || (emis_b != 0))
  188.             material.set_emissiveColor(emis_r, emis_g, emis_b);
  189.  
  190.         // shininess
  191.         shine = Float.valueOf(shininess.getText()).floatValue();
  192.         if(shine != 0.2f)
  193.             material.set_shininess(shine);
  194.  
  195.         // specularColor
  196.         spec_r = Float.valueOf(spec_x.getText()).floatValue();
  197.         spec_g = Float.valueOf(spec_y.getText()).floatValue();
  198.         spec_b = Float.valueOf(spec_z.getText()).floatValue();
  199.         if((spec_r != 0) || (spec_g != 0) || (spec_b != 0))
  200.             material.set_specularColor(spec_r, spec_g, spec_b);
  201.  
  202.         // transparency
  203.         shine = Float.valueOf(shininess.getText()).floatValue();
  204.         if(shine != 0)
  205.             material.set_shininess(shine);
  206.  
  207.         try
  208.         {
  209.             vrml_data.addNode(VrmlTypes.Material, material);
  210.         }
  211.         catch (NoSelectedNodeException e)
  212.         {
  213.             System.out.println("No node selected");
  214.         }
  215.  
  216.         return false;
  217.     }
  218.  
  219. }