home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1998 November
/
Chip_1998-11_cd.bin
/
tema
/
Cafe
/
VCSAMPL.BIN
/
Developer.java
< prev
next >
Wrap
Text File
|
1997-09-08
|
6KB
|
200 lines
/*
* Title: Conversing Applets
* Type: Applet
* Source: Developer.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 Developer applet class
*/
public class Developer 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 developerThread; //The main applet thread
/**
* Set up the applet layout and initialize variables
*/
public void init()
{
myMediator = new Mediator();
//{{INIT_CONTROLS
setLayout(null);
resize(452,110);
top = new java.awt.Label("I'm a Developer");
top.reshape(0,0,113,24);
add(top);
developer = new java.awt.Label("Developer says:");
developer.reshape(0,35,109,24);
add(developer);
developerTalk = new java.awt.TextField(18);
developerTalk.setEditable(false);
developerTalk.reshape(130,36,307,24);
add(developerTalk);
client = new java.awt.Label("The Client just said:");
client.reshape(0,70,126,24);
add(client);
clientTalk = new java.awt.TextField(18);
clientTalk.setEditable(false);
clientTalk.reshape(130,68,309,24);
add(clientTalk);
//}}
}
/**
* Start the main thread
*/
public void start()
{
developerThread = new Thread(this);
developerThread.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 developerThread = Thread.currentThread();
while(true)
{
try
{
developerThread.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 = "Have you seen Visual CafΘ?";
} else if((index <= 20) && (index >= 10))
{
label = "What's that?";
} else if((index <= 30) && (index > 20))
{
label = "Visual CafΘ is Great!";
} else if((index <= 40) && (index > 30))
{
label = "Visual CafΘ really helps.";
} else if((index <= 50) && (index > 40))
{
label = "I can make anything with Visual CafΘ.";
} else if((index <= 60) && (index > 50))
{
label = "The Visual CafΘ compiler is sooo FAST!";
} else if((index <= 70) && (index > 60))
{
label = "Debugging in Visual CafΘ is easy.";
} else if((index <= 80) && (index > 70))
{
label = "I see where you're going";
} else if((index <= 90) && (index > 80))
{
label = "Check out the snazzy class editor.";
} else if((index <= 100) && (index >90))
{
label = "Thank you!";
}
//Let the Mediator know what the developer said
myMediator.setDeveloperTalk(label);
//Find out what the client said
response = myMediator.getClientTalk();
}
/**
* A helper method which updates the UI elements
*/
public void takeAction()
{
developerTalk.setText(label);
clientTalk.setText(response);
}
//{{DECLARE_CONTROLS
java.awt.Label top;
java.awt.Label developer;
java.awt.TextField developerTalk;
java.awt.Label client;
java.awt.TextField clientTalk;
//}}
}