home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1997 May / PC Plus Super CD Issue 127 (May 1997).iso / vcafe / samples.bin / Developer.java < prev    next >
Encoding:
Java Source  |  1996-03-06  |  6.2 KB  |  216 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.            setLayout(new GridLayout(5,1));
  53.  
  54.            //{{INIT_CONTROLS
  55.            resize(189,128);
  56.            top=new Label("I'm a Developer");
  57.            add(top);
  58.            top.reshape(19,20,60,13);
  59.            developer=new Label("Developer says:");
  60.            add(developer);
  61.            developer.reshape(19,33,60,13);
  62.            developerTalk=new TextField(18);
  63.            add(developerTalk);
  64.            developerTalk.reshape(19,46,150,18);
  65.            client=new Label("The Client just said:");
  66.            add(client);
  67.            client.reshape(19,70,108,15);
  68.            clientTalk=new TextField(18);
  69.            add(clientTalk);
  70.            clientTalk.reshape(19,90,149,18);
  71.            //}}
  72.  
  73.            developerTalk.setEditable(false);
  74.            developerTalk.setForeground(Color.white);
  75.            clientTalk.setEditable(false);
  76.  
  77.         }
  78.  
  79.         /**
  80.          * Start the main thread
  81.          */
  82.  
  83.         public void start()
  84.         {
  85.            developerThread = new Thread(this);
  86.            developerThread.start();
  87.         }
  88.  
  89.         /**
  90.          * Handle any events here
  91.          */
  92.  
  93.         public boolean handleEvent(Event evt)
  94.         {
  95.             if(evt.target instanceof Button)
  96.             {
  97.                return true;
  98.             }
  99.            return false;
  100.          }
  101.  
  102.          /**
  103.           * The applet action takes place here. The applet waits, gets a
  104.           * random number from the Mediator, uses that to id a piece of
  105.           * conversation and then updates the UI elements.
  106.           */
  107.  
  108.          public void run()
  109.          {
  110.             Thread developerThread = Thread.currentThread();
  111.             while(true)
  112.             {
  113.                 try
  114.                 {
  115.                     developerThread.sleep(2000);
  116.                 } catch(Exception e)
  117.                 {
  118.                     System.out.println(e);
  119.                 }
  120.  
  121.                 //Keep a copy of the last random number
  122.                 myMediator.oldNumber = myMediator.theNumber;
  123.                 myMediator.theNumber = myMediator.genRandom();
  124.  
  125.                 checkWithMediator();
  126.  
  127.                 takeAction();
  128.  
  129.                 System.out.println("response =" + response);
  130.             }
  131.          }
  132.  
  133.          /**
  134.           * A helper function which uses the new Mediator random number
  135.           * to set the conversation text
  136.           */
  137.  
  138.          public synchronized void checkWithMediator()
  139.          {
  140.  
  141.             int index = myMediator.theNumber;
  142.  
  143.             System.out.println(index);
  144.  
  145.             if(index < 10)
  146.             {
  147.                 label = "Have you seen CafΘ?";
  148.             } else if((index <= 20) && (index >= 10))
  149.             {
  150.                 label = "What's that?";
  151.             } else if((index <= 30) && (index > 20))
  152.             {
  153.                 label = "CafΘ is Great!";
  154.             } else if((index <= 40) && (index > 30))
  155.             {
  156.                 label = "CafΘ studio really helps.";
  157.             } else if((index <= 50) && (index > 40))
  158.             {
  159.                 label = "I can make anything with CafΘ.";
  160.             } else if((index <= 60) && (index > 50))
  161.             {
  162.                 label = "The CafΘ compiler is sooo FAST!";
  163.             } else if((index <= 70) && (index > 60))
  164.             {
  165.                 label = "Debugging in CafΘ is easy.";
  166.             } else if((index <= 80) && (index > 70))
  167.             {
  168.                 label = "I see where you're going";
  169.             } else if((index <= 90) && (index > 80))
  170.             {
  171.                 label = "Check out the snazzy class editor.";
  172.             } else if((index <= 100) && (index >90))
  173.             {
  174.                 label = "Thank you!";
  175.             }
  176.  
  177.             //Let the Mediator know what the developer said
  178.             myMediator.setDeveloperTalk(label);
  179.  
  180.             //Find out what the client said
  181.             response = myMediator.getClientTalk();
  182.          }
  183.  
  184.  
  185.          /**
  186.           * A helper method which updates the UI elements
  187.           */
  188.  
  189.          public void takeAction()
  190.          {
  191.             developerTalk.setText(label);
  192.             clientTalk.setText(response);
  193.          }
  194.  
  195.         //{{DECLARE_CONTROLS
  196.         Label top;
  197.         Label developer;
  198.         TextField developerTalk;
  199.         Label client;
  200.         TextField clientTalk;
  201.         //}}
  202. }
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.