home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch12 / geometry / Box.java < prev    next >
Text File  |  1997-01-05  |  2KB  |  107 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 Box extends Geometry
  18. {
  19.     private float[] size = null;
  20.  
  21.     // constructor builds a default box node
  22.     public Box()
  23.     {
  24.         Browser b = Browser.getBrowser();
  25.  
  26.         if(b == null)
  27.             return;
  28.  
  29.         node = b.createVrmlFromString("Box {}")[0];
  30.     }
  31.  
  32.     public Box(Browser b)
  33.     {
  34.         if(b == null)
  35.             return;
  36.  
  37.         node = b.createVrmlFromString("Box {}")[0];
  38.     }
  39.  
  40.     public Box(float x, float y, float z)
  41.     {
  42.         Browser b = Browser.getBrowser();
  43.  
  44.         size = new float[3];
  45.  
  46.         size[0] = x;
  47.         size[1] = y;
  48.         size[2] = z;
  49.  
  50.         if(b == null)
  51.             return;
  52.  
  53.         node = b.createVrmlFromString("Box { size " +
  54.                                        x + " " +
  55.                                        y + " " +
  56.                                        z + "}")[0];
  57.         have_browser = true;
  58.     }
  59.  
  60.     public Box(Browser b, float x, float y, float z)
  61.     {
  62.         size = new float[3];
  63.  
  64.         size[0] = x;
  65.         size[1] = y;
  66.         size[2] = z;
  67.  
  68.         if(b == null)
  69.             return;
  70.  
  71.         node = b.createVrmlFromString("Box { size " +
  72.                                        x + " " +
  73.                                        y + " " +
  74.                                        z + "}")[0];
  75.         have_browser = true;
  76.     }
  77.  
  78.     public void finalize()
  79.     {
  80.         node = null;
  81.         size = null;
  82.     }
  83.  
  84.     public void writeToFile(PrintStream fp, int indent)
  85.     {
  86.         int    i;
  87.         StringBuffer buffer = new StringBuffer();
  88.  
  89.         for(i = 0; i < indent; i++)
  90.             buffer.append("  ");
  91.  
  92.         fp.print(buffer.toString() + "geometry ");
  93.         if(name != null)
  94.             fp.print("DEF " + name + " ");
  95.  
  96.         fp.print("Box {");
  97.  
  98.         if(size != null)
  99.             fp.print(" size " +
  100.                      size[0] + " " +
  101.                      size[1] + " " +
  102.                      size[2]);
  103.  
  104.         fp.println("}");
  105.     }
  106. }
  107.