JAVA RMI : A Shared Whiteboard
Hello
i just want to know how to compile this "java rmi" code of a shared whiteboard
and how could i'm implement/embed it in my website
this the code. for better explaination go through http://www.unix.org.ua/orelly/java-ent/dist/ch10_02.htm
Example 1: A Shared Whiteboard Client
Code:
package dcj.examples.Collaborative;
import dcj.util.Collaborative.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Hashtable;
import java.util.Properties;
import java.io.IOException;
import java.rmi.RemoteException;
public class WhiteboardUser extends RMICollaboratorImpl
implements MouseListener, MouseMotionListener
{
protected Hashtable lastPts = new Hashtable();
protected Component whiteboard;
protected Image buffer;
public WhiteboardUser(String name, Color color, String host, String mname)
throws RemoteException {
super(name);
Properties p = new Properties();
p.put("host", host);
p.put("mediatorName", mname);
connect(p);
getIdentity().setProperty("color", color);
System.out.println("color = " + color.getRed()
+ " " + color.getGreen() + " "
+ color.getBlue());
buildUI();
}
protected void buildUI() {
Frame f = new Frame();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
f.setLayout(gridbag);
f.addNotify();
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;
Canvas canvas1 = new java.awt.Canvas();
canvas1.setSize(240,180);
canvas1.setBackground(Color.white);
gridbag.setConstraints(canvas1, c);
f.add(canvas1);
String name = null;
try {
name = getIdentity().getName();
}
catch (Exception e) {
name = "unknown";
}
Label label1 = new java.awt.Label("Your name: " + name);
label1.setSize(100,30);
gridbag.setConstraints(label1, c);
f.add(label1);
f.setSize(240,210);
f.show();
whiteboard = canvas1;
whiteboard.addMouseListener(this);
whiteboard.addMouseMotionListener(this);
buffer = whiteboard.createImage(f.getSize().width,
f.getSize().height);
}
public void mousePressed(MouseEvent ev) {
Point evPt = ev.getPoint();
try {
broadcast("start", evPt);
}
catch (Exception e) {}
}
public void mouseReleased(MouseEvent ev) {
Point evPt = ev.getPoint();
try {
broadcast("end", evPt);
}
catch (Exception e) {}
}
public void mouseDragged(MouseEvent ev) {
Point evPt = ev.getPoint();
try {
broadcast("drag", evPt);
}
catch (Exception e) {
}
}
public void mouseExited(MouseEvent ev) {}
public void mouseMoved(MouseEvent ev) {}
public void mouseClicked(MouseEvent ev) {}
public void mouseEntered(MouseEvent ev) {}
public boolean notify(String tag, Object data, Identity src)
throws IOException, RemoteException {
Color origColor = null;
Color agentColor = null;
Graphics gr = buffer.getGraphics();
try {
agentColor = (Color)src.getProperty("color");
if (agentColor != null) {
gr.setColor(agentColor);
}
else {
System.out.println("No agent color available.");
}
}
catch (Exception exc) {
System.out.println("Exception while switching colors.");
exc.printStackTrace();
}
if (tag.compareTo("start") == 0) {
lastPts.put(src.getName(), data);
}
else if (tag.compareTo("drag") == 0) {
Point lastPt = (Point)lastPts.get(src.getName());
Point currPt = (Point)data;
gr.drawLine(lastPt.x, lastPt.y, currPt.x, currPt.y);
lastPts.put(src.getName(), data);
}
else if (tag.compareTo("end") == 0) {
Point lastPt = (Point)lastPts.get(src.getName());
Point currPt = (Point)data;
gr.drawLine(lastPt.x, lastPt.y, currPt.x, currPt.y);
lastPts.remove(src.getName());
}
whiteboard.getGraphics().drawImage(buffer, 0, 0, whiteboard);
return true;
}
}
Example 2: A Multithreaded Whiteboard Client
Code:
package dcj.examples.Collaborative;
import dcj.util.Collaborative.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Hashtable;
import java.util.Properties;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Vector;
public class ThreadedWhiteboardUser
extends RMICollaboratorImpl
implements java.awt.event.MouseListener,
java.awt.event.MouseMotionListener
{
protected Hashtable lastPts = new Hashtable();
protected Component whiteboard;
protected Image buffer;
protected CommHelper helper;
public ThreadedWhiteboardUser(String name, Color color,
String host, String mname)
throws RemoteException {
super(name, host, mname);
getIdentity().setProperty("color", color);
buildUI();
helper = new CommHelper(this);
helper.start();
}
protected void buildUI() {
Frame f = new Frame();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
f.setLayout(gridbag);
f.addNotify();
c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;
Canvas canvas1 = new java.awt.Canvas();
canvas1.setSize(240,180);
canvas1.setBackground(Color.white);
gridbag.setConstraints(canvas1, c);
f.add(canvas1);
String name = null;
try {
name = getIdentity().getName();
}
catch (Exception e) {
name = "unknown";
}
Label label1 = new java.awt.Label("Your name: " + name);
label1.setSize(100,30);
gridbag.setConstraints(label1, c);
f.add(label1);
f.setSize(240,210);
f.show();
whiteboard = canvas1;
whiteboard.addMouseListener(this);
whiteboard.addMouseMotionListener(this);
buffer = whiteboard.createImage(f.getSize().width,
f.getSize().height);
}
protected void nextLine(String agent, Point pt, Color c) {
Graphics g = buffer.getGraphics();
g.setColor(c);
Point lastPt = (Point)lastPts.get(agent);
g.drawLine(lastPt.x, lastPt.y, pt.x, pt.y);
whiteboard.getGraphics().drawImage(buffer, 0, 0, whiteboard);
}
public void mousePressed(MouseEvent ev) {
Point evPt = ev.getPoint();
try {
lastPts.put(getIdentity().getName(), evPt);
CommHelper.addMsg("start", evPt);
}
catch (Exception e) {}
}
public void mouseReleased(MouseEvent ev) {
Point evPt = ev.getPoint();
try {
nextLine(getIdentity().getName(), evPt,
(Color)getIdentity().getProperty("color"));
lastPts.remove(getIdentity().getName());
helper.addMsg("end", evPt);
}
catch (Exception e) {}
}
public void mouseDragged(MouseEvent ev) {
Point evPt = ev.getPoint();
try {
nextLine(getIdentity().getName(), evPt,
(Color)getIdentity().getProperty("color"));
lastPts.put(getIdentity().getName(), evPt);
helper.addMsg("drag", evPt);
}
catch (Exception e) {}
}
public void mouseExited(MouseEvent ev) {}
public void mouseMoved(MouseEvent ev) {}
public void mouseClicked(MouseEvent ev) {}
public void mouseEntered(MouseEvent ev) {}
public boolean notify(String tag, Object data, Identity src)
throws IOException, RemoteException {
// If this is our own event, ignore it since it's already been handled.
if (src.getName().compareTo(getIdentity().getName()) == 0) {
return true;
}
Color agentColor = null;
try {
agentColor = (Color)src.getProperty("color");
}
catch (Exception exc) {
System.out.println("Exception while getting color.");
exc.printStackTrace();
}
if (tag.compareTo("start") == 0) {
// First point along a path, save it and continue
lastPts.put(src.getName(), data);
}
else if (tag.compareTo("drag") == 0) {
// Next point in a path, draw a line from the last
// point to here, and save this point as the last point.
nextLine(src.getName(), (Point)data, agentColor);
lastPts.put(src.getName(), data);
}
else if (tag.compareTo("end") == 0) {
// Last point in a path, so draw the line and remove
// the last point.
nextLine(src.getName(), (Point)data, agentColor);
lastPts.remove(src.getName());
}
return true;
}
}
if someone can help me to embed it in my website i'm really appreciate it.