//******************************************************************************
// ThreadedHelloWorld.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;

//==============================================================================
// Main Class for applet ThreadedHelloWorld
//
//==============================================================================
public class ThreadedHelloWorld extends Applet implements Runnable
{
	int x;
	int y;
	int a;
	int b;
	int c;
	int d;
	// THREAD SUPPORT:
	//		m_ThreadedHelloWorld	is the Thread object for the applet
	//--------------------------------------------------------------------------
	private Thread	 m_ThreadedHelloWorld = null;


	// ThreadedHelloWorld Class Constructor
	//--------------------------------------------------------------------------
	public ThreadedHelloWorld()
	{
		x = 7;
		y = 13;
		a = 17;
		b = 47;
		c = 3;
		d = 91;
		// TODO: Add constructor code here
	}

	// APPLET INFO SUPPORT:
	//		The getAppletInfo() method returns a string describing the applet's
	// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: ThreadedHelloWorld\r\n" +
		       "Author: David Mendoza Flores and Grant Thornton LLP\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	// The init() method is called by the AWT when an applet is first loaded or
	// reloaded.  Override this method to perform whatever initialization your
	// applet needs, such as initializing data structures, loading images or
	// fonts, creating frame windows, setting the layout manager, or adding UI
	// components.
    //--------------------------------------------------------------------------
	public void init()
	{
        // If you use a ResourceWizard-generated "control creator" class to
        // arrange controls in your applet, you may want to call its
        // CreateControls() method from within this method. Remove the following
        // call to resize() before adding the call to CreateControls();
        // CreateControls() does its own resizing.
        //----------------------------------------------------------------------
    	resize(750, 370);

		// TODO: Place additional initialization code here
	}

	// Place additional applet clean up code here.  destroy() is called when
	// when you applet is terminating and being unloaded.
	//-------------------------------------------------------------------------
	public void destroy()
	{
		// TODO: Place applet cleanup code here
	}

	// ThreadedHelloWorld Paint Handler
	//--------------------------------------------------------------------------
	//int count = 0;
	public void paint(Graphics g)
	{
		// TODO: Place applet paint code here
		//g.drawString("Running: " + Math.random(), 10, 20);
		
		//count++;
		//g.drawString("Times painted: " + count, 10 ,20);
        //g.drawString("Times painted: " + count, 40 ,40);
		//g.drawString("Times painted: " + count, 60 ,60);
	
		g.drawString("Moving String ", x, y);
		x+=a - c;
		y+=b - d;
		if( x > 400) x = y;
		if( y > 250) y = a; 
		
		g.drawString("Can you catch me? ", a, b);
		a+=x-y;
		b+=12;
		if( a > 730) a = y;
		if( b > 350) b = c; 
		
		g.drawString("You betcha! ", c, d);
		c = y - a;
		d = x - b;
		if (c > 555) c = x;
		if (d > 370) d = a;
	}

	//		The start() method is called when the page containing the applet
	// first appears on the screen. The AppletWizard's initial implementation
	// of this method starts execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void start()
	{
		if (m_ThreadedHelloWorld == null)
		{
			m_ThreadedHelloWorld = new Thread(this);
			m_ThreadedHelloWorld.start();
		}
		// TODO: Place additional applet start code here
	}
	
	//		The stop() method is called when the page containing the applet is
	// no longer on the screen. The AppletWizard's initial implementation of
	// this method stops execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void stop()
	{
		if (m_ThreadedHelloWorld != null)
		{
			m_ThreadedHelloWorld.stop();
			m_ThreadedHelloWorld = null;
		}

		// TODO: Place additional applet stop code here
	}

	// THREAD SUPPORT
	//		The run() method is called when the applet's thread is started. If
	// your applet performs any ongoing activities without waiting for user
	// input, the code for implementing that behavior typically goes here. For
	// example, for an applet that performs animation, the run() method controls
	// the display of images.
	//--------------------------------------------------------------------------
	public void run()
	{
		while (true)
		{
			try
			{
				repaint();
				// TODO:  Add additional thread-specific code here
				Thread.sleep(25);
			}
			catch (InterruptedException e)
			{
				// TODO: Place exception-handling code here in case an
				//       InterruptedException is thrown by Thread.sleep(),
				//		 meaning that another thread has interrupted this one
				stop();
			}
		}
	}



	// TODO: Place additional applet code here

}
