// PaintApplet.java -- simple paint applet using connection manager // // import java.awt.*; import java.applet.*; import java.io.*; import java.util.Vector; import java.util.Enumeration; import java.util.StringTokenizer; public class PaintApplet extends ConnectionManagerClientApplet { MyPaintFrame frame; public void init () { name = "Paint"; frame = new MyPaintFrame (this, "Whiteboard"); super.init (); } public void close () { frame.close (); super.close (); } // override server input handler inherited from Client public void notifyInput (String s) { StringTokenizer st = new StringTokenizer (s); String cmd = st.nextToken(); if (cmd.equals("erase")) frame.erase(); else if (cmd.equals ("draw")) try { Vector points = new Vector (100); Color c = new Color (Integer.parseInt (st.nextToken())); while (st.hasMoreTokens()) { int x = Integer.parseInt (st.nextToken()); int y = Integer.parseInt (st.nextToken()); points.addElement (new Dimension (x, y)); } frame.draw (c, points); } catch (Exception e); } public void sendOutput (String s) { super.sendOutput (s); } } class MyPaintFrame extends PaintFrame { PaintApplet parent; MyPaintFrame (PaintApplet p, String title) { super (title); parent = p; } public void notifyErase () { parent.sendOutput ("erase\n"); } public void notifyDraw (Color c, Vector points) { Enumeration e = points.elements(); String s = "draw " + String.valueOf(c.getRGB()); while (e.hasMoreElements()) { Dimension d = (Dimension)e.nextElement(); s = s + " " + String.valueOf(d.width) + " " + String.valueOf(d.height); } parent.sendOutput (s + "\n"); } public void notifyClose () { parent.close (); } }