home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch15 / VRMLBrowser.java < prev    next >
Text File  |  1997-01-27  |  3KB  |  101 lines

  1. import dnx.lr.*;
  2. import dnx.lr.app.*;
  3. import java.awt.*;
  4. import java.io.IOException;
  5. import java.net.URL;
  6.  
  7.  
  8. public class VRMLBrowser extends Frame {
  9.  
  10.     protected ApplicationDocument document;
  11.     protected View3D              view;
  12.  
  13.  
  14.     public VRMLBrowser(String title) {
  15.         super(title);
  16.         setLayout(new BorderLayout(5, 5));
  17.         Panel p = new Panel();
  18.         p.setLayout(new BorderLayout());
  19.         p.add("West",   new Label("Location: ", Label.LEFT));
  20.         p.add("Center", new TextField());
  21.         add("North", p);
  22.     } 
  23.  
  24.     public boolean action(Event event, Object arg) {
  25.         if (event.target instanceof TextField) {
  26.             System.err.println("Loading new URL: " + arg);
  27.             try {
  28.                 readScene((String) arg);
  29.             }
  30.             catch (VRMLSyntaxException e) {
  31.                 System.out.println ("Invalid VRML Syntax in file: " + arg);
  32.                 return false;
  33.             }
  34.             catch (IOException e) {
  35.                 System.out.println ("Unable to load: " + e);
  36.                 return false;
  37.             }
  38.             return true;
  39.         }
  40.         return false;
  41.     } 
  42.   
  43.     public boolean handleEvent(Event event) {
  44.         if (event.id == Event.WINDOW_DESTROY) {
  45.             System.exit(0);
  46.         }
  47.         return super.handleEvent(event);
  48.     }
  49.  
  50.     public Insets insets() {
  51.         return new Insets(super.insets().top   +5, super.insets().left +5,
  52.                           super.insets().bottom+5, super.insets().right+5);
  53.     }
  54.  
  55.     public void readScene(String arg) throws VRMLSyntaxException,
  56.                                              IOException {
  57.         URL url = new URL(ApplicationDocument.getCurrentDirectory(), arg);
  58.         Scene scene = new Scene(new ReadAction(url));
  59.         ApplicationDocument doc = new ApplicationDocument(scene);
  60.         if (view == null) {
  61.             document = doc;
  62.             view = new BrowserView3D(document);
  63.         }
  64.         else {
  65.             document.stop();
  66.             document = doc;
  67.             view = new BrowserView3D(document);
  68.             add("Center", view.getComponent());
  69.             validate();
  70.             document.start();
  71.         }
  72.     }
  73.  
  74.     public static void main(String[] args) {
  75.         VRMLBrowser frame = new VRMLBrowser("Minimal VRML Browser");
  76.  
  77.         if (args.length != 1) {
  78.             System.err.println("Usage:  java VRMLBrowser <url>");
  79.             System.exit(0);
  80.         }
  81.  
  82.         try {
  83.             frame.readScene(args[0]);
  84.         }
  85.         catch (VRMLSyntaxException e) {
  86.             System.out.println ("Invalid VRML Syntax in file: " + args[0]);
  87.             return;
  88.         }
  89.         catch (IOException e) {
  90.             System.out.println ("Unable to load: " + e);
  91.             return;
  92.         }
  93.  
  94.         frame.add("Center", frame.view.getComponent());
  95.         frame.resize(440, 340);
  96.         frame.show();
  97.  
  98.         frame.document.start();
  99.     }
  100. }
  101.