home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / Cone.java < prev    next >
Text File  |  1997-01-02  |  3KB  |  162 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 Cone 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 _bottom = true;
  25.  
  26.     // constructor builds a default box node
  27.     public Cone()
  28.     {
  29.         Browser b = Browser.getBrowser();
  30.  
  31.         if(b == null)
  32.             return;
  33.  
  34.         node = b.createVrmlFromString("Cone {}")[0];
  35.     }
  36.  
  37.     public Cone(Browser b)
  38.     {
  39.         if(b == null)
  40.             return;
  41.  
  42.         node = b.createVrmlFromString("Cone {}")[0];
  43.     }
  44.  
  45.     public Cone(float radius,
  46.                 float height,
  47.                 boolean showSide,
  48.                 boolean showBottom)
  49.     {
  50.         String vrml_string = "Cone {";
  51.  
  52.         if(radius != 1)
  53.             vrml_string += " bottomRadius " + radius;
  54.  
  55.         if(height != 2)
  56.             vrml_string += " height " + height;
  57.  
  58.         if(!showSide)
  59.             vrml_string += " side FALSE";
  60.  
  61.         if(!showBottom)
  62.             vrml_string += "bottom FALSE";
  63.  
  64.         vrml_string += " }";
  65.  
  66.         Browser b = Browser.getBrowser();
  67.  
  68.         if(b != null)
  69.         {
  70.             node = b.createVrmlFromString(vrml_string)[0];
  71.             have_browser = true;
  72.         }
  73.  
  74.         _radius = radius;
  75.         _height = height;
  76.         _side = showSide;
  77.         _bottom = showBottom;
  78.  
  79.         isDefault = false;
  80.     }
  81.  
  82.     public Cone(Browser b,
  83.                 float radius,
  84.                 float height,
  85.                 boolean showSide,
  86.                 boolean showBottom)
  87.     {
  88.         String vrml_string = "Cone {";
  89.  
  90.         if(radius != 1)
  91.             vrml_string += " bottomRadius " + radius;
  92.  
  93.         if(height != 2)
  94.             vrml_string += " height " + height;
  95.  
  96.         if(!showSide)
  97.             vrml_string += " side FALSE";
  98.  
  99.         if(!showBottom)
  100.             vrml_string += "bottom FALSE";
  101.  
  102.         vrml_string += " }";
  103.  
  104.         if(b != null)
  105.         {
  106.             node = b.createVrmlFromString(vrml_string)[0];
  107.             have_browser = true;
  108.         }
  109.  
  110.         _radius = radius;
  111.         _height = height;
  112.         _side = showSide;
  113.         _bottom = showBottom;
  114.  
  115.         isDefault = false;
  116.     }
  117.  
  118.     public void finalize()
  119.     {
  120.         node = null;
  121.     }
  122.  
  123.     public void writeToFile(PrintStream fp, int indent)
  124.     {
  125.         int    i;
  126.         StringBuffer buffer = new StringBuffer();
  127.  
  128.         for(i = 0; i < indent; i++)
  129.             buffer.append("  ");
  130.  
  131.         fp.print(buffer.toString() + "geometry ");
  132.         if(name != null)
  133.             fp.print("DEF " + name + " ");
  134.  
  135.  
  136.         if(isDefault)
  137.         {
  138.             fp.println("Cone {}");
  139.             return;
  140.         }
  141.  
  142.         fp.println("Cone {");
  143.  
  144.         if(_radius != 1)
  145.             fp.println(buffer.toString() +
  146.                        " bottomRadius " +
  147.                        _radius);
  148.  
  149.         if(_height != 2)
  150.             fp.println(buffer.toString() +
  151.                        " height " +
  152.                        _height);
  153.         if(!_side)
  154.             fp.println(buffer.toString() + " side FALSE");
  155.  
  156.         if(!_bottom)
  157.             fp.println(buffer.toString() + " bottom FALSE ");
  158.  
  159.         fp.println(buffer.toString() + "}");
  160.     }
  161. }
  162.