home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch22 / chess.java next >
Text File  |  1997-03-12  |  5KB  |  151 lines

  1. // The Chess applet
  2.  
  3. // Written by Bernie Roehl, March 1997
  4.  
  5. import java.applet.*;
  6. import java.util.*;
  7. import java.awt.*;
  8. import java.io.*;
  9. import netscape.javascript.JSObject;
  10. import vrml.external.Browser;
  11. import vrml.external.Node;
  12. import vrml.external.field.*;
  13. import vrml.external.exception.*;
  14.  
  15. public class chess
  16.     extends Applet
  17.     implements Runnable, EventOutObserver {
  18.  
  19.     static boolean move_made = false;
  20.  
  21.     Thread myThread;
  22.     Browser browser;          // the VRML browser
  23.  
  24.     public String getAppletInfo() {
  25.         return "VRML Chess Interface " + 0.1 + ", written by Bernie Roehl";
  26.     }
  27.  
  28.     Piece pieces[] = {
  29.         new Piece("BlackRook1"), new Piece("BlackKnight1"),
  30.         new Piece("BlackBishop1"),
  31.         new Piece("BlackKing"), new Piece("BlackQueen"),
  32.         new Piece("BlackBishop2"), new Piece("BlackKnight2"),
  33.         new Piece("BlackRook2"),
  34.         new Piece("BlackPawn1"), new Piece("BlackPawn2"),
  35.         new Piece("BlackPawn3"), new Piece("BlackPawn4"), 
  36.         new Piece("BlackPawn5"), new Piece("BlackPawn6"),
  37.         new Piece("BlackPawn7"), new Piece("BlackPawn8"), 
  38.         new Piece("WhiteRook1"), new Piece("WhiteKnight1"),
  39.         new Piece("WhiteBishop1"),
  40.         new Piece("WhiteKing"), new Piece("WhiteQueen"),
  41.         new Piece("WhiteBishop2"), new Piece("WhiteKnight2"),
  42.         new Piece("WhiteRook2"),
  43.         new Piece("WhitePawn1"), new Piece("WhitePawn2"), new Piece("WhitePawn3"),
  44.         new Piece("WhitePawn4"), 
  45.         new Piece("WhitePawn5"), new Piece("WhitePawn6"), new Piece("WhitePawn7"),
  46.         new Piece("WhitePawn8")
  47.     };
  48.  
  49.     public void start() {
  50.         browser = getBrowser();
  51.         for (int i = 0; i < pieces.length; ++i)
  52.             pieces[i].restart(browser, this);
  53.         if (myThread == null)
  54.             myThread = new Thread(this);
  55.         myThread.start();
  56.     }
  57.  
  58.     public void stop() {
  59.        if (myThread != null)
  60.            myThread.stop();
  61.     }
  62.  
  63.     public void run() {
  64. System.out.println("run");
  65.         while (true) {
  66.             if (move_made) {
  67.                 System.out.println("Move made!");
  68.                 move_made = false;
  69.             }
  70.         }
  71.     }
  72.  
  73.     private Browser getBrowser() {
  74.         Browser browser = Browser.getBrowser();
  75.         if (browser == null) {
  76.             JSObject win = JSObject.getWindow(this);
  77.             if (win != null) {
  78.                 JSObject doc = (JSObject) win.getMember("document");
  79.                 JSObject embeds = (JSObject) doc.getMember("embeds");
  80.                 JSObject applets = (JSObject) doc.getMember("applets");
  81.                 JSObject frames = (JSObject) doc.getMember("frames");
  82.                 for (int i = 0; i < 10; ++i) {
  83.                     browser = (Browser) embeds.getSlot(0);
  84.                     if (browser != null)
  85.                         break;
  86.                     try { Thread.sleep(500); }
  87.                     catch (InterruptedException e) { }
  88.                 }
  89.             }
  90.         }
  91.         return browser;
  92.     }
  93.  
  94.     private int n = 0;  // used to count callbacks
  95.  
  96.     public void callback(EventOut event, double time, Object userData) {
  97.         EventOutSFBool active = (EventOutSFBool) event;
  98.         if (active.getValue())
  99.             return;
  100.         Piece piece = (Piece) userData;
  101.         piece.updateLocation();
  102.         float[] loc = piece.getLocation();
  103.         int rank = 4 + -(int) Math.floor(loc[1] / 0.12);
  104.         int file = 5 + (int) Math.floor(loc[0] / 0.12);
  105. System.out.print(piece.getName() + " moved to ");
  106. System.out.println("[" + rank + ", " + file + "]");
  107.         String otherpiece = "Black" + piece.getName().substring(5);
  108. System.out.println("reponse: move " + otherpiece + " to " + (9-rank) + ", " + file);
  109.         Node other = browser.getNode(otherpiece);
  110.         EventInSFVec3f set_location = (EventInSFVec3f) other.getEventIn("set_location");
  111.         loc[1] = -loc[1];
  112.         set_location.setValue(loc);
  113.         move_made = true;
  114.     }
  115. }
  116.  
  117. class Piece {
  118.     String name;
  119.     Node node;
  120.     float[] initialLocation = new float[3];
  121.     float[] currentLocation = new float[3];
  122.     String getName() { return name; }
  123.     float[] getLocation() {
  124.         float[] loc = new float[2];
  125.         loc[0] = initialLocation[0] + currentLocation[0];
  126.         loc[1] = initialLocation[2] + currentLocation[1];
  127.         return loc;
  128.     }
  129.     public Piece(String nam) {
  130.         name = nam;
  131.     }
  132.     public void restart(Browser browser, EventOutObserver observer) {
  133.         node = null;
  134.         while (node == null) {
  135.             try { node = browser.getNode(name); }
  136.             catch (InvalidNodeException e) { }
  137.         }
  138.         EventOutSFVec3f loc = (EventOutSFVec3f) node.getEventOut("initialLocation");
  139.         initialLocation = loc.getValue();
  140.         updateLocation();
  141.         EventOutSFBool isActive = (EventOutSFBool) node.getEventOut("isActive");
  142.         isActive.advise(observer, this);
  143.     }
  144.     public void updateLocation() {
  145.         EventOutSFVec3f loc = (EventOutSFVec3f) node.getEventOut("location_changed");
  146.         currentLocation = loc.getValue();
  147.     }
  148. }
  149.  
  150.  
  151.