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

  1. /*
  2.  * Title: Conversing Applets
  3.  * Type: Applet
  4.  * Source: Developer.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. import java.applet.Applet;
  32. import java.awt.*;
  33.  
  34. /**
  35.  * The Developer applet class
  36.  */
  37.  
  38. public class Developer extends Applet implements Runnable
  39. {
  40.         Mediator myMediator;            //Local copy of the Mediator object
  41.         String label, response;         //Various applet text variables
  42.         int theNumber;                  //Used to track a random number
  43.         Thread developerThread;               //The main applet thread
  44.  
  45.         /**
  46.          * Set up the applet layout and initialize variables
  47.          */
  48.  
  49.         public void init()
  50.         {
  51.            myMediator = new Mediator();
  52.  
  53.            //{{INIT_CONTROLS
  54.         setLayout(null);
  55.         setSize(452,110);
  56.         top = new java.awt.Label("I'm a Developer");
  57.         top.setBounds(0,0,113,24);
  58.         add(top);
  59.         developer = new java.awt.Label("Developer says:");
  60.         developer.setBounds(0,35,109,24);
  61.         add(developer);
  62.         developerTalk = new java.awt.TextField(18);
  63.         developerTalk.setEditable(false);
  64.         developerTalk.setBounds(130,36,307,24);
  65.         add(developerTalk);
  66.         client = new java.awt.Label("The Client just said:");
  67.         client.setBounds(0,70,126,24);
  68.         add(client);
  69.         clientTalk = new java.awt.TextField(18);
  70.         clientTalk.setEditable(false);
  71.         clientTalk.setBounds(130,68,309,24);
  72.         add(clientTalk);
  73.         //}}
  74.  
  75.  
  76.         }
  77.  
  78.         /**
  79.          * Start the main thread
  80.          */
  81.  
  82.         public void start()
  83.         {
  84.            developerThread = new Thread(this);
  85.            developerThread.start();
  86.         }
  87.  
  88.  
  89.          /**
  90.           * The applet action takes place here. The applet waits, gets a
  91.           * random number from the Mediator, uses that to id a piece of
  92.           * conversation and then updates the UI elements.
  93.           */
  94.  
  95.          public void run()
  96.          {
  97.             Thread developerThread = Thread.currentThread();
  98.             while(true)
  99.             {
  100.                 try
  101.                 {
  102.                     developerThread.sleep(2000);
  103.                 } catch(Exception e)
  104.                 {
  105.                     System.out.println(e);
  106.                 }
  107.  
  108.                 //Keep a copy of the last random number
  109.                 myMediator.oldNumber = myMediator.theNumber;
  110.                 myMediator.theNumber = myMediator.genRandom();
  111.  
  112.                 checkWithMediator();
  113.  
  114.                 takeAction();
  115.  
  116.             }
  117.          }
  118.  
  119.          /**
  120.           * A helper function which uses the new Mediator random number
  121.           * to set the conversation text
  122.           */
  123.  
  124.          public synchronized void checkWithMediator()
  125.          {
  126.  
  127.             int index = myMediator.theNumber;
  128.  
  129.             if(index < 10)
  130.             {
  131.                 label = "Have you seen Visual CafΘ?";
  132.             } else if((index <= 20) && (index >= 10))
  133.             {
  134.                 label = "What's that?";
  135.             } else if((index <= 30) && (index > 20))
  136.             {
  137.                 label = "Visual CafΘ is Great!";
  138.             } else if((index <= 40) && (index > 30))
  139.             {
  140.                 label = "Visual CafΘ really helps.";
  141.             } else if((index <= 50) && (index > 40))
  142.             {
  143.                 label = "I can make anything with Visual CafΘ.";
  144.             } else if((index <= 60) && (index > 50))
  145.             {
  146.                 label = "The Visual CafΘ compiler is sooo FAST!";
  147.             } else if((index <= 70) && (index > 60))
  148.             {
  149.                 label = "Debugging in Visual CafΘ is easy.";
  150.             } else if((index <= 80) && (index > 70))
  151.             {
  152.                 label = "I see where you're going";
  153.             } else if((index <= 90) && (index > 80))
  154.             {
  155.                 label = "Check out the snazzy class editor.";
  156.             } else if((index <= 100) && (index >90))
  157.             {
  158.                 label = "Thank you!";
  159.             }
  160.  
  161.             //Let the Mediator know what the developer said
  162.             myMediator.setDeveloperTalk(label);
  163.  
  164.             //Find out what the client said
  165.             response = myMediator.getClientTalk();
  166.          }
  167.  
  168.  
  169.          /**
  170.           * A helper method which updates the UI elements
  171.           */
  172.  
  173.          public void takeAction()
  174.          {
  175.             developerTalk.setText(label);
  176.             clientTalk.setText(response);
  177.          }
  178.  
  179.         //{{DECLARE_CONTROLS
  180.     java.awt.Label top;
  181.     java.awt.Label developer;
  182.     java.awt.TextField developerTalk;
  183.     java.awt.Label client;
  184.     java.awt.TextField clientTalk;
  185.     //}}
  186. }
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.