// DrawJPanel2.java
// Demos a JPanel used as a dedicated drawing area
// Listens to its own mouse events
// Uses anonymous inner class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DrawJPanel2 extends JPanel implements Runnable {
	private int width, height;
	private int n = 0; 		// counter for balls
	private int redX, redY;	// coordinates of red ball
	private int greenX, greenY;
	private int blueX, blueY;
	private int redXMove = ( int ) ( 5 * Math.random( ) + 1 );
	private int redYMove = ( int ) ( 5 * Math.random( ) + 1 );
	private int greenXMove = ( int ) ( 5 * Math.random( ) + 1 );
	private int greenYMove = ( int ) ( 5 * Math.random( ) + 1 );
	private int blueXMove = ( int ) ( 5 * Math.random( ) + 1 );
	private int blueYMove = ( int ) ( 5 * Math.random( ) + 1 );

	private boolean isRedBall = false;	
	private boolean isGreenBall = false;
	private boolean isBlueBall = false;
	private Thread threadRed = new Thread( this, "Red" );
	private Thread threadGreen  = new Thread( this, "Green" );
	private Thread threadBlue = new Thread( this, "Blue" );

	public void run( ) {
		Thread executingThread;

		while ( true ) {
			// sleep for .01 seconds
			try {
				Thread.sleep( 10 );
			}
			catch ( InterruptedException ex ) {
				ex.printStackTrace( );
			}
			executingThread = Thread.currentThread( );

			if ( executingThread == threadRed ) {
				redX += redXMove;
				if ( redX >= 562 || redX <= 4 )
					redXMove *= -1;		// reverse direction if wall is hit

				redY += redYMove;
				if ( redY >= 288 || redY <= 4 )
					redYMove *= -1;		// reverse direction if wall is hit
			
				repaint( );
			}

			if ( executingThread == threadGreen ) {
				greenX += greenXMove;
				if ( greenX >= 562 || greenX <= 4 )
					greenXMove *= -1;

				greenY += greenYMove;
				if ( greenY >= 288 || greenY <= 4 )
					greenYMove *= -1;
			
				repaint( );
			}

			if ( executingThread == threadBlue ) {
				blueX += blueXMove;
				if ( blueX >= 562 || blueX <= 4 )
					blueXMove *= -1;

				blueY += blueYMove;
				if ( blueY >= 288 || blueY <= 4 )
					blueYMove *= -1;
			
				repaint( );
			}
		}
	}

	public DrawJPanel2( ) {
		addMouseListener(
			new MouseAdapter( ) {
				public void mousePressed( MouseEvent e ) {
					n++;			// increment counter
					if ( n == 1 ) {
						isRedBall = true;
						redX = e.getX( );
						redY = e.getY( );
						threadRed.start( );			// launches thread
					}

					if ( n == 2 ) {
						isGreenBall = true;
						greenX = e.getX( );
						greenY = e.getY( );
						threadGreen.start( );
					}

					if ( n == 3 ) {
						isBlueBall = true;
						blueX = e.getX( );
						blueY = e.getY( );
						threadBlue.start( );
					}

					repaint( );
				}
			}
		);
	}

	public void drawingStuff( int w, int h ){
		// receives two size arguments from applet
		width = w;
		height = h;
		repaint( );		// calls paintComponent
	}

	// all of the actual drawing takes place here
	public void paintComponent( Graphics g ) {
		// call to super class
		super.paintComponent( g );
		// my drawing code
		g.setColor( Color.blue );
		g.drawRect( 3, 3, 614, 340 );
		g.drawRect( 4, 4, 612, 338 );

		if ( isRedBall ) {
			g.setColor( Color.red );
			g.fillOval( redX, redY, width, height );
		}

		if ( isGreenBall ) {
			g.setColor( Color.green );
			g.fillOval( greenX, greenY, width, height );
		}

		if ( isBlueBall ) {
			g.setColor( Color.blue );
			g.fillOval( blueX, blueY, width, height );
		}
	}
}