home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
VCSAMPL.BIN
/
Client.java
< prev
next >
Wrap
Text File
|
1997-06-02
|
6KB
|
208 lines
/*
* Title: Conversing Applets
* Type: Applet
* Source: Client.java
* Application Description:
* The Conversing Applets project provides a simple and effective way
* to allow applets to communicate within the same "environment", i.e.
* within the same browser page.
*
* This is done using a Mediator class as the background manager and
* other visible classes which display results. The Mediator class uses
* static variables to keep track of all the events taking place in
* these visible applets.
*
* In this example, two visible applets are used along with the one
* Mediator class. The Developer and Client class generate text strings which
* represent conversation. The Mediator object(s) take those text
* strings and distribute them to the other applet (Developer to Client and
* Client to Developer).
*
* The result is two distinct applet objects transmitting data back and
* forth. This sort of project can be very useful in developing more
* interesting Web pages as well as Web page applets which gather
* information about usage, etc. The Mediator class concept can be
* built upon to meet the needs of more specific Java-based Web
* applications.
*
* Symantec Corporation.
*/
import java.applet.Applet;
import java.awt.*;
/**
* The Client applet class
*/
public class Client extends Applet implements Runnable
{
Mediator myMediator; //Local copy of the Mediator object
String label, response; //Various applet text variables
int theNumber; //Used to track a random number
Thread clientThread; //The main applet thread
/**
* Set up the applet layout and initialize variables
*/
public void init()
{
myMediator = new Mediator();
//{{INIT_CONTROLS
setLayout(null);
addNotify();
resize(448,110);
clientTalk = new java.awt.TextField(18);
clientTalk.setEditable(false);
clientTalk.reshape(170,38,268,24);
add(clientTalk);
client = new java.awt.Label("Client says:");
client.reshape(0,35,80,24);
add(client);
top = new java.awt.Label("I'm a Client");
top.reshape(0,0,89,24);
add(top);
developer = new java.awt.Label("The Developer just said:");
developer.reshape(0,70,153,24);
add(developer);
developerTalk = new java.awt.TextField(17);
developerTalk.setEditable(false);
developerTalk.reshape(170,68,268,24);
add(developerTalk);
//}}
}
/**
* Start the main thread
*/
public void start()
{
clientThread = new Thread(this);
clientThread.start();
}
/**
* The applet action takes place here. The applet waits, gets a
* random number from the Mediator, uses that to id a piece of
* conversation and then updates the UI elements.
*/
public void run()
{
Thread clientThread = Thread.currentThread();
while(true)
{
try
{
clientThread.sleep(2000);
} catch(Exception e)
{
System.out.println(e);
}
//Keep a copy of the last random number
myMediator.oldNumber = myMediator.theNumber;
myMediator.theNumber = myMediator.genRandom();
checkWithMediator();
takeAction();
}
}
/**
* A helper function which uses the new Mediator random number
* to set the conversation text
*/
public synchronized void checkWithMediator()
{
int index = myMediator.theNumber;
if(index < 10)
{
label = "Isn't Visual CafΘ great?";
} else if((index <= 20) && (index >= 10))
{
label = "The Visual CafΘ compiler is sooo FAST!";
} else if((index <= 30) && (index > 20))
{
label = "Check out the snazzy class editor.";
} else if((index <= 40) && (index > 30))
{
label = "Debugging in Visual CafΘ is easy.";
} else if((index <= 50) && (index > 40))
{
label = "Visual CafΘ is Great!";
} else if((index <= 60) && (index > 50))
{
label = "What do you mean by that?";
} else if((index <= 70) && (index > 60))
{
label = "I want to make sure you use Visual CafΘ.";
} else if((index <= 80) && (index > 70))
{
label = "Isn't that the truth";
} else if((index <= 90) && (index > 80))
{
label = "Wow, really!!!";
} else if((index <= 100) && (index >90))
{
label = "That's very cool";
}
//Let the Mediator know what the client said
myMediator.setClientTalk(label);
//Find out what the developer said
response = myMediator.getDeveloperTalk();
}
/**
* A helper method which updates the UI elements
*/
public void takeAction()
{
clientTalk.setText(label);
developerTalk.setText(response);
}
//{{DECLARE_CONTROLS
java.awt.TextField clientTalk;
java.awt.Label client;
java.awt.Label top;
java.awt.Label developer;
java.awt.TextField developerTalk;
//}}
}