import coffeedraw.*;
public class Driver extends WindowController {
private World world;
public static void main(String[] args) {
new Driver().startController(800, 600);
}
public void begin() {
world = new World(canvas);
}
public void onMousePress(Location point) {
world.onMousePress(point);
}
public void onMouseDrag(Location point) {
world.onMouseDrag(point);
}
}
Copy
import coffeedraw.*;
import java.awt.Color;
import java.util.*;
public class World extends ActiveObject implements ClientListener {
private DrawingCanvas canvas;
private Client client;
private RandomDoubleGenerator rando = new RandomDoubleGenerator(0, 360);
private RandomIntGenerator rando2 = new RandomIntGenerator(0, 255);
private RandomIntGenerator rando3 = new RandomIntGenerator(0, 1);
WeakHashMap objects = new WeakHashMap();
public World(DrawingCanvas c) {
canvas = c;
Network.configure("CoffeePacketTester", ServerListener.ECHO, this);
start();
}
public void run() {
while(true) {
pause(1);
}
}
public void onMousePress(Location point) {
if(this.client == null) return;
LocationPacket packet = new LocationPacket(point);
ColorPacket packet2 = new ColorPacket(new Color(rando2.nextValue(), rando2.nextValue(), rando2.nextValue()));
RotationPacket packet3 = new RotationPacket(rando.nextValue());
VisibilityPacket packet4 = new VisibilityPacket(rando3.nextValue() == 1 ? true : false);
String message = Packet.serialize(packet);
String message2 = Packet.serialize(packet2);
String message3 = Packet.serialize(packet3);
String message4 = Packet.serialize(packet4);
client.sendMessage(message);
client.sendMessage(message2);
client.sendMessage(message3);
client.sendMessage(message4);
}
public void onMouseDrag(Location point) {
if(this.client == null) return;
LocationPacket packet = new LocationPacket(point);
ColorPacket packet2 = new ColorPacket(new Color(rando2.nextValue(), rando2.nextValue(), rando2.nextValue()));
RotationPacket packet3 = new RotationPacket(rando.nextValue());
VisibilityPacket packet4 = new VisibilityPacket(rando3.nextValue() == 1 ? true : false);
String message = Packet.serialize(packet);
String message2 = Packet.serialize(packet2);
String message3 = Packet.serialize(packet3);
String message4 = Packet.serialize(packet4);
client.sendMessage(message);
client.sendMessage(message2);
client.sendMessage(message3);
client.sendMessage(message4);
}
public void clientConnected(Client client) {
this.client = client;
}
public void messageReceived(Client client, String message) {
if(client.equals(this.client)) this.client = client;
Packet packet = Packet.deserialize(message);
if(packet instanceof SpawnPacket) {
SpawnPacket spawnPacket = (SpawnPacket) packet;
Resizable2DInterface object = null;
try {
object = DrawingCanvas.getObject(spawnPacket, canvas);
} catch(Exception e) {
// Don't print the stack trace.
// Silently catch.
}
objects.put(spawnPacket.getID(), object);
} else if(packet instanceof DespawnPacket) {
DespawnPacket dPacket = (DespawnPacket) packet;
objects.get(dPacket.getID()).removeFromCanvas();
objects.remove(dPacket.getID());
}
if(packet instanceof LocationPacket) {
LocationPacket locPacket = (LocationPacket) packet;
objects.get(locPacket.getID()).moveTo(locPacket.getLocation());
} else if(packet instanceof SizePacket) {
SizePacket sizePacket = (SizePacket) packet;
objects.get(sizePacket.getID()).setSize(sizePacket.getWidth(), sizePacket.getHeight());
} else if(packet instanceof ColorPacket) {
ColorPacket colPacket = (ColorPacket) packet;
objects.get(colPacket.getID()).setColor(colPacket.getColor());
} else if(packet instanceof RotationPacket) {
RotationPacket rotPacket = (RotationPacket) packet;
objects.get(rotPacket.getID()).setRotation(rotPacket.getRotation());
} else if(packet instanceof VisibilityPacket) {
VisibilityPacket visPacket = (VisibilityPacket) packet;
//objects.get(visPacket.getID()).setVisible(visPacket.isVisible());
}
}
public void clientDisconnected(Client client) {
this.client = null;
}
}
Copy