home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / WDESAMPL.BIN / Client.java < prev    next >
Text File  |  1998-02-26  |  6KB  |  207 lines

  1. /*
  2.  * Title: Conversing Applets
  3.  * Type: Applet
  4.  * Source: Client.java
  5.  * Application Description:
  6.  * The Conversing Applets project provides a simple and effective way
  7.  * to allow applets to communicate within the same "environment", i.e.
  8.  * within the same browser page.
  9.  *
  10.  * This is done using a Mediator class as the background manager and
  11.  * other visible classes which display results. The Mediator class uses
  12.  * static variables to keep track of all the events taking place in
  13.  * these visible applets.
  14.  *
  15.  * In this example, two visible applets are used along with the one
  16.  * Mediator class. The Developer and Client class generate text strings which
  17.  * represent conversation.  The Mediator object(s) take those text
  18.  * strings and distribute them to the other applet (Developer to Client and
  19.  * Client to Developer).
  20.  *
  21.  * The result is two distinct applet objects transmitting data back and
  22.  * forth. This sort of project can be very useful in developing more
  23.  * interesting Web pages as well as Web page applets which gather
  24.  * information about usage, etc.  The Mediator class concept can be
  25.  * built upon to meet the needs of more specific Java-based Web
  26.  * applications.
  27.  *
  28.  * Symantec Corporation.
  29.  */
  30.  
  31.  
  32. import java.applet.Applet;
  33. import java.awt.*;
  34.  
  35. /**
  36.  * The Client applet class
  37.  */
  38.  
  39. public class Client extends Applet implements Runnable
  40. {
  41.         Mediator myMediator;            //Local copy of the Mediator object
  42.         String label, response;         //Various applet text variables
  43.         int theNumber;                  //Used to track a random number
  44.         Thread clientThread;             //The main applet thread
  45.  
  46.  
  47.         /**
  48.          * Set up the applet layout and initialize variables
  49.          */
  50.  
  51.         public void init()
  52.         {
  53.            myMediator = new Mediator();
  54.  
  55.            //{{INIT_CONTROLS
  56.         setLayout(null);
  57.         setSize(448,110);
  58.         clientTalk = new java.awt.TextField(18);
  59.         clientTalk.setEditable(false);
  60.         clientTalk.setBounds(170,38,268,24);
  61.         add(clientTalk);
  62.         client = new java.awt.Label("Client says:");
  63.         client.setBounds(0,35,80,24);
  64.         add(client);
  65.         top = new java.awt.Label("I'm a Client");
  66.         top.setBounds(0,0,89,24);
  67.         add(top);
  68.         developer = new java.awt.Label("The Developer just said:");
  69.         developer.setBounds(0,70,153,24);
  70.         add(developer);
  71.         developerTalk = new java.awt.TextField(17);
  72.         developerTalk.setEditable(false);
  73.         developerTalk.setBounds(170,68,268,24);
  74.         add(developerTalk);
  75.         //}}
  76.  
  77.         }
  78.  
  79.  
  80.         /**
  81.          * Start the main thread
  82.          */
  83.  
  84.         public void start()
  85.         {
  86.            clientThread = new Thread(this);
  87.            clientThread.start();
  88.         }
  89.  
  90.  
  91.  
  92.  
  93.          /**
  94.           * The applet action takes place here. The applet waits, gets a
  95.           * random number from the Mediator, uses that to id a piece of
  96.           * conversation and then updates the UI elements.
  97.           */
  98.  
  99.          public void run()
  100.          {
  101.             Thread clientThread = Thread.currentThread();
  102.             while(true)
  103.             {
  104.                 try
  105.                 {
  106.                     clientThread.sleep(2000);
  107.                 } catch(Exception e)
  108.                 {
  109.                     System.out.println(e);
  110.                 }
  111.  
  112.                 //Keep a copy of the last random number
  113.                 myMediator.oldNumber = myMediator.theNumber;
  114.                 myMediator.theNumber = myMediator.genRandom();
  115.  
  116.                 checkWithMediator();
  117.  
  118.                 takeAction();
  119.  
  120.             }
  121.          }
  122.  
  123.  
  124.          /**
  125.           * A helper function which uses the new Mediator random number
  126.           * to set the conversation text
  127.           */
  128.  
  129.          public synchronized void checkWithMediator()
  130.          {
  131.  
  132.             int index = myMediator.theNumber;
  133.  
  134.  
  135.             if(index < 10)
  136.             {
  137.                 label = "Isn't Visual CafΘ great?";
  138.             } else if((index <= 20) && (index >= 10))
  139.             {
  140.                 label = "The Visual CafΘ compiler is sooo FAST!";
  141.             } else if((index <= 30) && (index > 20))
  142.             {
  143.                 label = "Check out the snazzy class editor.";
  144.             } else if((index <= 40) && (index > 30))
  145.             {
  146.                 label = "Debugging in Visual CafΘ is easy.";
  147.             } else if((index <= 50) && (index > 40))
  148.             {
  149.                 label = "Visual CafΘ is Great!";
  150.             } else if((index <= 60) && (index > 50))
  151.             {
  152.                 label = "What do you mean by that?";
  153.             } else if((index <= 70) && (index > 60))
  154.             {
  155.                 label = "I want to make sure you use Visual CafΘ.";
  156.             } else if((index <= 80) && (index > 70))
  157.             {
  158.                 label = "Isn't that the truth";
  159.             } else if((index <= 90) && (index > 80))
  160.             {
  161.                 label = "Wow, really!!!";
  162.             } else if((index <= 100) && (index >90))
  163.             {
  164.                 label = "That's very cool";
  165.             }
  166.  
  167.             //Let the Mediator know what the client said
  168.             myMediator.setClientTalk(label);
  169.  
  170.             //Find out what the developer said
  171.             response = myMediator.getDeveloperTalk();
  172.          }
  173.  
  174.  
  175.  
  176.          /**
  177.           * A helper method which updates the UI elements
  178.           */
  179.  
  180.          public void takeAction()
  181.          {
  182.             clientTalk.setText(label);
  183.             developerTalk.setText(response);
  184.          }
  185.  
  186.         //{{DECLARE_CONTROLS
  187.     java.awt.TextField clientTalk;
  188.     java.awt.Label client;
  189.     java.awt.Label top;
  190.     java.awt.Label developer;
  191.     java.awt.TextField developerTalk;
  192.     //}}
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.