
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

import java.net.*;
import java.io.*;

public class DmgPoetry4 extends Applet implements MouseMotionListener, MouseListener, Runnable {

    Image offscrImg;          //sets up virtual screen
    Image img;
    Graphics offscrG;    //get a graphic content

    Tile2 tileList[];
    int maxTiles = 70;
	int wordIndex = 0;
	int hotTile = -1;
    String wordList[] = {"I", "burn", "a", "about", "above", "ache", "after", "all",
        "always", "am", "an", "and", "are", "arm", "as", "ask", "at",
        "away", "bare", "be", "beat", "beauty", "bed", "behind",
        "beneath", "bitter", "black", "blood", "blow", "blue", "boil",
        "boy", "breast", "but", "he", "she", "by", "can", "car", "chain",
        "chant", "club", "cook", "cool", "could", "crush", "cry", "drove",
        "day", "death", "delicate", "delicious", "diamond", "did", "do",
        "dream", "dress", "drive", "drool", "drunk", "easy", "eat",
        "ed", "egg", "enormous", "er", "es", "essential", "est", "fall",
        "fast", "feet", "felt", "fiddle", "finger", "flood", "fluff",
        "for", "forest", "frantic", "friend", "from", "ful", "garden",
        "gift", "girl", "go", "goddess", "gone", "gorgeous", "gown",
        "hair", "has", "have", "he", "head", "heave", "her", "here",
        "him", "his", "hit", "honey", "hot", "how", "if", "in", "ing",
        "iron", "is", "it", "juice", "knife", "lake", "language",
        "languid", "lather", "lazy", "leave", "leg", "less", "let",
        "lick", "lie", "life", "light", "like", "live", "love",
        "luscious", "lust", "ly", "mad", "man", "may", "me", "mean",
        "meat", "men", "milk", "mist", "moan", "moon", "most", "mother",
        "music", "must", "my", "near", "need", "ness", "never", "next",
        "no", "none", "not", "of", "on", "one", "only", "or", "orb",
        "our", "ous", "out", "over", "pant", "part", "peach", "petal",
        "picture", "pink", "place", "play", "please", "pole", "pound",
        "power", "produce", "puppy", "purple", "put", "r", "rain",
        "raw", "read", "recall", "red", "repulsive", "rip", "road",
        "rock", "rose", "run", "rust", "s", "sad", "said", "sausage",
        "say", "scream", "sea", "see", "shadow", "shake", "she",
        "shine", "ship", "shot", "show", "sing", "sit", "size", "skin",
        "sky", "sleep", "smear", "smooth", "so", "soar", "some",
        "sordid", "spray", "spring", "stare", "still", "stop", "suit",
        "summer", "sun", "sweat", "sweet", "swim", "take", "tell",
        "the", "their", "them", "then", "there", "these", "they",
        "thing", "think", "those", "though", "through", "time", "tiny",
        "to", "top", "trip", "trudge", "true", "ugly", "under", "up",
        "urge", "use", "vision", "void", "want", "was", "watch",
        "water", "wax", "we", "weak", "were", "what", "when", "whisper",
        "white", "who", "why", "will", "wind", "winter", "with",
        "woman", "women", "worship", "would", "y", "yet", "you"};


    Thread myThread;  //the receiving thread

    Socket connection;
    DataOutputStream output;
    DataInputStream  input;  // the input stream

//*******************
    public void init(){

	addMouseMotionListener( this );
	addMouseListener( this );

	offscrImg=createImage ( 770, 600 );          //sets up virtual screen
	offscrG = offscrImg.getGraphics();      //get a graphic content   

	img = getImage( getDocumentBase(), "frig.jpg" );
            
	// connect to server
	runClient();
  	
	// initialize tiles
	initTiles();
    }

//*********************************
    public void start() {
        if( myThread == null ) {
            myThread = new Thread( this );
            myThread.start();
        }
    }

//*********************************
    public void stop() {
        if(myThread != null) {
            myThread.stop();
            myThread = null;
        }
    }

//*********************************
    public void run() {
	while( myThread != null ) {
	    try {
		receiveData();
		myThread.sleep( 20 );
	    } catch(InterruptedException ioe) {
		stop();
	    }
	}
    }

//*********************************
    public void paint(Graphics g) {
	offscrG.drawImage (img,0,0,this);
	paintTiles( offscrG );
	g.drawImage (offscrImg,0,0,this);

    }

//*********************************
    public void paintTiles (Graphics g) {
	for ( int i=0; i < maxTiles; i++ ) {
	    g.setColor (Color.white);
	    tileList[i].paint(g);
	}
    }

//********************************
    public void mouseDragged( MouseEvent e ) {
	if ( hotTile >= 0 ) {
	    sendData( e.getX(), e.getY() );
	}
    }

//********************************
    public void mousePressed( MouseEvent e ) {
	hotTile = testInorOut ( e.getX(), e.getY() );
    }

//********************************
    public void mouseReleased( MouseEvent e ) {}

//********************************
    public void mouseMoved (MouseEvent e){}  // unused mouse functions
    public void mouseClicked (MouseEvent e){}
    public void mouseEntered (MouseEvent e){}
    public void mouseExited (MouseEvent e){}

//********************************
    private int testInorOut (int x, int y) {
	for ( int i = maxTiles - 1; i >= 0; i-- ) {
	    if ( tileList[i].isinTile(x,y) )
		{
		    return i;
		}    
	}
	return -1;
    }

//************************
   public void runClient(){

       try{
	   connection = new Socket( InetAddress.getByName("128.95.103.42"), 
				    5001);

	   output = new DataOutputStream(connection.getOutputStream() );
	   System.out.println("output connection established with server:" +
			      connection.getInetAddress().getHostName());

	   input = new DataInputStream(connection.getInputStream() );
	   System.out.println("input connection established with server:" +
			      connection.getInetAddress().getHostName());

		// initialize tile locations

       } catch(IOException ioe){
	   System.out.println("No connection");
	   noServer();
       }
   }

//**************************************
    public void receiveData() {
	try{
	    int xIn = input.readInt();
	    int yIn = input.readInt();
	    int indexIn = input.readInt();

	    // move tile
	    tileList[ indexIn ].move( xIn, yIn );
	    Graphics g = getGraphics();
	    paint( g );

	} catch(IOException ioe) {
	    System.out.println("No connection");
	}
    }

//**********************************************
    public void sendData( int xOut, int yOut ) {
	try {
	    output.writeInt( xOut );
	    output.writeInt( yOut );
	    output.writeInt( hotTile );
	    output.flush();
	    //System.out.println("Client has sent = " + xout + " " + yout);
	} catch(IOException ioe) {
	    System.out.println("No connection");
	    noServer();
	}
    }

//**********************************************
    private void initTiles() {
	tileList = new Tile2[maxTiles];
	try{
	    for ( int i = 0; i < maxTiles; i++ ) {
		int xIn = input.readInt();
		int yIn = input.readInt();
		int indexIn = input.readInt();
		tileList[ indexIn ] = new Tile2( xIn, yIn, 
						 wordList[wordIndex] );
		wordIndex = wordIndex + 4;
		// System.out.println( "new tile " + i + ": " );
		// System.out.println( "  x: " + xIn + " y: " + yIn 
		//	    + " index: " + indexIn 
		//	    + " word: " + wordList[wordIndex] );
	    }
	    
	} catch(IOException ioe) {
	    System.out.println( "No connection" );
	    noServer();
	}
    }

//**********************************************
    private void noServer() {
	Graphics g = getGraphics();
	offscrG.setColor( Color.white );
	offscrG.drawImage (img,0,0,this);
	offscrG.drawString( "unable to connect to server", 100, 200 );
	g.drawImage (offscrImg,0,0,this);
	stop();
    }

//**********************************************
}
