Networking
Download and include the library
BorlandNetwork
In action:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Scanner;
/**
* Class t - write a description of the class here
*
* @author (your name)
* @version (a version number)
*/
public class simpleChat extends BorlandNetwork
{
BTextField textField1=new BTextField(29);
JButton button1= new JButton ("Test Button");
JTextArea testArea=new JTextArea(5,10);
int x;
public void initialize()
{
//setWidth(300);
//setFullScreen();
setAnimation(false);
askIP(false);
setBackgroundColor(Color.blue);
//setBackgroundImage("i.jpg");
addItem(textField1);
addItem(button1);
addItem(testArea);
}
public void buttonPressed (Component c)
{
if (c==button1)
{
echo("button pressed");
connection.sendObject(textField1.getText());
}
}
public void receiveUpdate(Object packet)
{
String text=(String)packet;
testArea.append(text);
}
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
char key=e.getKeyChar();
}
public void paintScreen (Graphics g)
{
}
//the method below will get called every frame.
//its argument g is the temporary graphics layer
public void update(Graphics g)
{
x++;
g.setColor(Color.green);
g.fillRect(40+x,50,20,20);
}
public static void main (String args[]){
try{ BorlandNetwork.startClient((BorlandNetwork)(new Object() { }.getClass().getEnclosingClass().newInstance())); }
catch (Exception e){e.printStackTrace();}
}
public static void startClient() {
try{ BorlandNetwork.startClient((BorlandNetwork)(new Object() { }.getClass().getEnclosingClass().newInstance())); }
catch (Exception e){e.printStackTrace();}
}
public static void startServer() {
BorlandNetwork.startServer(25);
}
}
Assignments:
- Create an applet IsArrow that will have a textfield that will
say arrow or not arrow when they hit a key.
- Create an applet hideImage that when the space bar is pressed,
an image
will show or hid on the screen.
|