home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / enxle1f6 / src / games / battle / client / clientapplet / clientupdater.java < prev   
Encoding:
Java Source  |  1996-08-14  |  2.3 KB  |  110 lines

  1. /*
  2.  * @(#)ClientUpdater.java
  3.  */
  4. package games.Battle.client.ClientApplet;
  5.  
  6. import java.lang.Thread;
  7.  
  8. /**
  9.  * The client updater is simple a thread that sleeps until somebody
  10.  * wakes it up to redraw the board, and then it goes back to sleep 
  11.  * again.
  12.  *
  13.  * @author Alex Nicolaou
  14.  * @author Jay Steele
  15.  */
  16. public class ClientUpdater implements Runnable {
  17.  
  18.     /**
  19.      * The board to obtain update information from.
  20.      */
  21.     ClientBoard board;
  22.  
  23.     /**
  24.      * The current "look" of the client game board.
  25.      */
  26.     ClientLook look;
  27.  
  28.     /**
  29.      * The thread whose sole purpose is to graphically update the client.
  30.      */
  31.     Thread updaterThread;
  32.  
  33.  
  34.     /**
  35.      *
  36.      */
  37.     boolean running = true;
  38.  
  39.     /**
  40.      * Construct a ClientUpdater with the given ClientBoard 
  41.      * and ClientLook. 
  42.      * @param board the ClientBoard this updater will update from
  43.      * @param look the ClientLook this updater will update with
  44.      */
  45.     public ClientUpdater(ClientBoard board, ClientLook look) {
  46.         this.board = board;
  47.         this.look = look;
  48.  
  49.         // start the updater thread
  50.         updaterThread = new Thread(this);
  51.         updaterThread.start();
  52.     }
  53.  
  54.     /**
  55.      * Set the look for this updater.
  56.      * @param look the look instance to reference from now on
  57.      */
  58.     public synchronized void setLook(ClientLook look) {
  59.         this.look = look;
  60.     }
  61.  
  62.     /**
  63.      * Called to wake up the thread and update the board.
  64.      */
  65.     public void update() {
  66.         updaterThread.resume();
  67.     }
  68.  
  69.     /**
  70.      * Physically update the contents of the display with the board
  71.      * information.
  72.      */
  73.     protected synchronized void updateLook() {
  74.         look.update(board);
  75.         look.updateDisplay();
  76.     }
  77.  
  78.     /**
  79.      * The run method executed by the thread. The method basically
  80.      * loops, performing a) board updates and then b) suspending
  81.      * itself. The updates is "woken" up by a call to "update";
  82.      */
  83.     public void run() {
  84.         long oldtime = 0;
  85.         long newtime = 0;
  86.         long diff = 0;
  87.         while (running) {
  88.  
  89.             // I will suspend until somebody wakes me 
  90.             // up to update the board
  91.             updaterThread.suspend();
  92.  
  93.             oldtime = System.currentTimeMillis();
  94.             // I'm awake, now go and update the board
  95.             updateLook();
  96.             newtime = System.currentTimeMillis();
  97.             diff = newtime - oldtime;
  98.             System.out.println("It took "+diff+" milliseconds to update the client");
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Stop the updater thread from executing.
  104.      */
  105.     public void stop() {
  106.         running = false;
  107.         updaterThread.stop();
  108.     }
  109. }
  110.