import java.awt.*;
import java.net.*;
import java.io.*;

public class juggler extends Panel implements Runnable{
// some global variables we can use anywhere inside the juggler class
int move = 0;
ball b,r,g;
Thread t = null;
String url = "";

     // program begins here, puts our panel into a frame
      public static void main(String args[]) {
            Frame frame1 = new Frame("juggler frame1");
            juggler j = new juggler();
            frame1.add(j);
            frame1.reshape(0, 0, Toolkit.getDefaultToolkit().getScreenSize().width, 
                                      Toolkit.getDefaultToolkit().getScreenSize().height); 
            frame1.show();
            j.init();
    }
        
	public void init(){
		  setLayout(null);  // this means we will position things with reshape()
		  
		  b = new ball(Color.blue);
		  add(b);
		  b.reshape(170,170,100,100);
		  
		  r = new ball();
	       add(r);
		  r.reshape(50,50,100,100);
		  
		  g = new ball(Color.green,"this is green");
		  add(g);
		  g.reshape(50,50,100,120);
	}

	public boolean handleEvent(Event e){
		 if(e.id == Event.MOUSE_DRAG){
		            if(move==1) (r).move(e.x,e.y);
		      else if(move==2) (g).move(e.x,e.y);
		      else if(move==3) (b).move(e.x,e.y);
		 }
		 else if(e.id == Event.MOUSE_DOWN){
			 if(e.target instanceof ball){
				 if((ball)e.target == r)
				 	 move = 1;
				 else if((ball)e.target == g)
					 move = 2;
				 else if((ball)e.target == b)
					 move = 3;
				 else move = 0;
			 }
		 }
		 else if(e.id == Event.MOUSE_UP){
			 move = 0;
			 url = "http://199.100.100.1/data2.php?x="+e.x+"&y="+e.y;
			 start();
		 }
         return true;
	}
	
	public void start(){
		t = new Thread(this);
		t.start();
	}
	
	public void stop(){
		t = null;
	}
	
	 public void run(){
	        if(url=="") return;
	        try{
			     URLConnection urlcon =  (new URL(url).openConnection());
			     urlcon.setDoOutput(false);
			     urlcon.setUseCaches(false);
			     urlcon.connect();
			     DataInputStream is = new DataInputStream(urlcon.getInputStream());
			     System.out.println(is.readLine());
			     if(is!=null) is.close();
			     System.out.println(url);
			}catch(Exception e){
				System.out.println("what happened? "+e);
			}
	 }
}


class ball extends Canvas {
 Color c = Color.red;
 String lbl = "";
 
   // the ball class has three different "constructors"
    public ball(Color notDefault, String label){
 	   super();
 	   c = notDefault;
	   lbl = label;
    }
    public ball(Color notDefault){
	   super();
	   c = notDefault;
    }
    public ball(){
	    super();
    }
	
    public void paint(Graphics g){
        g.setColor(c);
        g.fillOval(0, 0, size().width, size().width);
        g.drawString(lbl, 0, size().height);
    }
    
    public void move(int x, int y){
         // this function passes the move() request up
         // to the component-level function, but it adjusts
         // the coordinates a little so we get the right "feel"
         // when we click and drag
    	 super.move(x - size().width/2 ,  y - size().height/2);
    }
}
