home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / Cylinder.java < prev    next >
Text File  |  1997-01-02  |  3KB  |  176 lines

  1. // VRML Generator
  2. // Copyright Justin Couch 1996
  3. //
  4. // Chapter 13: Late Night VRML 2.0 and Java
  5. //
  6. // Box class
  7.  
  8. package geometry;
  9.  
  10. import java.io.*;
  11. import java.awt.*;
  12. import vrml.external.*;
  13. import vrml.external.field.*;
  14. import vrml.external.exception.*;
  15. import geometry.Geometry;
  16.  
  17. public class Cylinder extends Geometry
  18. {
  19.     private boolean    isDefault = true;
  20.  
  21.     private float _radius = 1;
  22.     private float _height = 2;
  23.     private boolean _side = true;
  24.     private boolean _top = true;
  25.     private boolean _bottom = true;
  26.  
  27.     // constructor builds a default box node
  28.     public Cylinder()
  29.     {
  30.         Browser b = Browser.getBrowser();
  31.  
  32.         if(b == null)
  33.             return;
  34.  
  35.         node = b.createVrmlFromString("Cylinder {}")[0];
  36.     }
  37.  
  38.     public Cylinder(Browser b)
  39.     {
  40.         if(b == null)
  41.             return;
  42.  
  43.         node = b.createVrmlFromString("Cylinder {}")[0];
  44.     }
  45.  
  46.     public Cylinder(float radius,
  47.                     float height,
  48.                     boolean showSide,
  49.                     boolean showTop,
  50.                     boolean showBottom)
  51.     {
  52.         String vrml_string = "Cylinder {";
  53.  
  54.         if(radius != 1)
  55.             vrml_string += " bottomRadius " + radius;
  56.  
  57.         if(height != 2)
  58.             vrml_string += " height " + height;
  59.  
  60.         if(!showSide)
  61.             vrml_string += " side FALSE";
  62.  
  63.         if(!showTop)
  64.             vrml_string += " top FALSE";
  65.  
  66.         if(!showBottom)
  67.             vrml_string += "bottom FALSE";
  68.  
  69.         vrml_string += " }";
  70.  
  71.         Browser b = Browser.getBrowser();
  72.  
  73.         if(b != null)
  74.         {
  75.             node = b.createVrmlFromString(vrml_string)[0];
  76.             have_browser = true;
  77.         }
  78.  
  79.         _radius = radius;
  80.         _height = height;
  81.         _side = showSide;
  82.         _top = showTop;
  83.         _bottom = showBottom;
  84.  
  85.         isDefault = false;
  86.     }
  87.  
  88.     public Cylinder(Browser b,
  89.                     float radius,
  90.                     float height,
  91.                     boolean showSide,
  92.                     boolean showTop,
  93.                     boolean showBottom)
  94.     {
  95.         String vrml_string = "Cylinder {";
  96.  
  97.         if(radius != 1)
  98.             vrml_string += " bottomRadius " + radius;
  99.  
  100.         if(height != 2)
  101.             vrml_string += " height " + height;
  102.  
  103.         if(!showSide)
  104.             vrml_string += " side FALSE";
  105.  
  106.         if(!showTop)
  107.             vrml_string += " top FALSE";
  108.  
  109.         if(!showBottom)
  110.             vrml_string += "bottom FALSE";
  111.  
  112.         vrml_string += " }";
  113.  
  114.         if(b != null)
  115.         {
  116.             node = b.createVrmlFromString(vrml_string)[0];
  117.             have_browser = true;
  118.         }
  119.  
  120.         _radius = radius;
  121.         _height = height;
  122.         _side = showSide;
  123.         _top = showTop;
  124.         _bottom = showBottom;
  125.  
  126.         isDefault = false;
  127.     }
  128.  
  129.     public void finalize()
  130.     {
  131.         node = null;
  132.     }
  133.  
  134.     public void writeToFile(PrintStream fp, int indent)
  135.     {
  136.         int    i;
  137.         StringBuffer buffer = new StringBuffer();
  138.  
  139.         for(i = 0; i < indent; i++)
  140.             buffer.append("  ");
  141.  
  142.         fp.print(buffer.toString() + "geometry ");
  143.         if(name != null)
  144.             fp.print("DEF " + name + " ");
  145.  
  146.  
  147.         if(isDefault)
  148.         {
  149.             fp.println("Cylinder {}");
  150.             return;
  151.         }
  152.  
  153.         fp.println("Cylinder {");
  154.  
  155.         if(_radius != 1)
  156.             fp.println(buffer.toString() +
  157.                        " bottomRadius " +
  158.                        _radius);
  159.  
  160.         if(_height != 2)
  161.             fp.println(buffer.toString() +
  162.                        " height " +
  163.                        _height);
  164.         if(!_side)
  165.             fp.println(buffer.toString() + " side FALSE");
  166.  
  167.         if(!_top)
  168.             fp.println(buffer.toString() + " top FALSE");
  169.  
  170.         if(!_bottom)
  171.             fp.println(buffer.toString() + " bottom FALSE ");
  172.  
  173.         fp.println(buffer.toString() + "}");
  174.     }
  175. }
  176.