import java.awt.*;
import java.net.*;

public class communicationDemo extends java.applet.Applet implements Runnable {
Image pixelImage;
StringBuffer mouseStroke;
Thread t;
int ox, oy;
   public void init(){
          pixelImage = createImage(512,512);  
   }
   public boolean mouseDown(Event evt, int x, int y){
          mouseStroke = new StringBuffer();
          mouseStroke.append(x+","+y+",");
          ox = x;
          oy = y;
          return true;
   }
   public boolean mouseDrag(Event evt, int x, int y){
         mouseStroke.append(x+","+y+",");
         Graphics tmp = pixelImage.getGraphics();
         tmp.drawLine(x,y,ox,oy);
         tmp.dispose();
         ox = x;
         oy = y;
         repaint();
   		 return true;
   }
   public boolean mouseUp(Event evt, int x, int y){
         start();
         return true;
   }
   public void update(Graphics g){
         paint(g);
   }
   public void paint(Graphics g){
   	     g.drawImage(pixelImage,0,0,this);
   
   }
   public void start(){  
      t = new Thread(this);
      t.start();
   }
   public void stop(){
      t = null;
   }
   public void run(){
       if(mouseStroke!=null){ 
          try{ getAppletContext().showDocument(
          		new URL(getCodeBase()+"receiveData.php?"+mouseStroke.toString())   ); }
          catch(Exception e){ System.out.println(e); }
       }
   }
}

/*
This version causes the applet to implement a new "thread" when the mouse is released. During this 
thread the applet sends its stroke data to the script receiveData.php using QUERY_STRING.
<?php
// The file should be called receiveData.php and placed along side the
// communicationDemo.class file on the server.
print($QUERY_STRING);
?>
*/
