import java.awt.*;
import java.net.*;

public class Banner3 extends java.applet.Applet implements Runnable 

{	

/****************************/
/*public integer variables  */
/****************************/
	public int width; 
	public int height;
	public int y = 0; //current position y
	public int j = 0; //counter
	public int i = 0; //counter
	public int fheight = 0; //font height
	public int xstart = 0;  //position at which x starts
	public int ystart = 0;  //position at which y starts
	public int xstop = 0;   //position at which x stops
	public int ystop = 0;   //position at which y stops
	public int msgwidth;    //width of msg window
/***********************/
/*public integer arrays*/
/***********************/
	public int[] catfwidth; //integer array of font widths for the category strings
	public int[] msgfwidth; //integer array of font widths for the message strings
	public int[] msgx;      //integer array of the center of message
        public int[] catx;      //integer array of the center of category

	boolean slept = false;
	// Other control variables
/*************************/
/*static object variables*/
/*************************/
	static FontMetrics metrics;
	static Graphics gc = null;
	static Graphics g;
	static Image offscreen;
	static Rectangle apprect;
/********************************************/
/*static variables parsed in ParseArgs class*/
/********************************************/
	static String fontname;
	static int fontsize;
	static int fontstyle;
	static Font font;
   static Color msgbgcolor;        //message area backgroud color
   static Color[] catbgcolor;        //category area background color
   static Color tempColor;    
	static Color linkColor;		//color of msgtext when mouse enter
	static Color msgtextcolor;	//message text color
   static Color[] cattextcolor;      //category text color

         static int totalmessages;	//total number of messages
	static String[] msgtext;	//array of message strings
	static String[] cattext;	//array of category strings
	static String[] textURL;	//array of URL strings

	static String valign;
	static String align;
	static String direction;

	static int delay;			//what does this do?
	int speed;			//what does this do?
	static int catwidth;		//width of the category space
	static int pauseDelay;		//amount of time of delay(milliseconds)
	static int xdelta;			//change in x
	static int ydelta;			//change in y

	private Thread juicer;		//our thread

/*************/
/*init method*/
/*************/
	public void init() 

        {       ParseArgs3 pa = new ParseArgs3(this);

		g = getGraphics();

      tempColor = msgtextcolor;

		//initialize the arrays
		catfwidth = new int [totalmessages];
		catx = new int [totalmessages];
        	msgfwidth = new int [totalmessages];
		msgx = new int[totalmessages];
		msgfwidth = new int[totalmessages];

		//get the width and height of the applet
		apprect = bounds();
		width = apprect.width;
		height = apprect.height;
		resize(width, height);

		offscreen = createImage(width, height);

		//instantiate the font object
		font = new Font(fontname, fontstyle, fontsize);
		metrics = g.getFontMetrics(font);
		fheight = metrics.getMaxAscent() - metrics.getMaxDescent() + 2;
		msgwidth = width - catwidth;

        	for( i=0; i < totalmessages; i++) 

		{	catfwidth[i] = metrics.stringWidth(cattext[i]);

			catx[i] = ( (catwidth - catfwidth[i]) / 2);

			

			msgfwidth[i] = metrics.stringWidth(msgtext[i]);

			msgx[i] = ( (msgwidth - msgfwidth[i]) / 2 + catwidth);

		}

		i=0;



		try 

		{	if(direction.equalsIgnoreCase("BottomTop")) 

			{	ystart = y = height + fheight;

				ystop = -fheight;

				int dist = Math.abs(ystop - ystart);

				if(dist % ydelta != 0)

				ystop -= ydelta - (dist % ydelta);

				xstart = msgx[0];

				xstop = xdelta = 0;

				ydelta = -ydelta;

			}

			

			else if(direction.equalsIgnoreCase("TopBottom")) 

			{	ystart = y = -fheight;

				ystop = height + fheight;

				int dist = Math.abs(ystop - ystart);

				if(dist % ydelta != 0) 

					ystop += ydelta - (dist % ydelta);

				xstart = msgx[0];

				xstop = xdelta = 0;

			}

		}

		catch (Exception e) 

		{	ystart = y = -fheight;

				ystop = height + fheight;

				int dist = Math.abs(ystop - ystart);

				if(dist % ydelta != 0) 

					ystop += ydelta - (dist % ydelta);

				xstart = msgx[0];

				xstop = xdelta = 0;

		}

	}



/**************/

/*start method*/

/**************/

	public void start() 

	{	if(juicer == null) 

		{	juicer = new Thread(this);

			juicer.start();

		}

	}

   

/*************/

/*stop method*/

/*************/

	public void stop() 

	{	if ( (juicer != null) && (juicer.isAlive()) ) 

		{	juicer.stop();

			juicer = null;

		}

	}



/************/

/*run method*/

/************/

	public void run() 

	{

		while (juicer != null) 

		{	update(g);

			try

			{	juicer.sleep(1); 

			} 

			catch (InterruptedException e) 

			{	System.out.println("something wrong with juicer");

			}



			//test for middle

			if ((y == (height - fheight) / 2 + fheight - 1)

				&& slept == false) 

			{	try 

				{	juicer.sleep(pauseDelay);

				}	

				catch (InterruptedException e)

				{	System.out.println("juicer pauseDelay");

				} 

				slept = true;

			}

				

			// haven't reached the top

			if(y != ystop) y += ydelta; 

			

			// have reached the top

			else 

			{	y = ystart;  // reset y

				

            // go back to first message if necessary

				if (i == (totalmessages - 1) ) 

				{	i = 0; 

					slept = false; 

				}

				

            // go to next message

				else

				{	i++;

				slept = false;

				} 

			 }

		  }

	   }

	   

	   

/*****************/

/*update method  */

/*****************/

	public void update(Graphics g)

	{	paint(g); 

	}



/**************/
/*paint method*/

/**************/

	public void paint(Graphics g) 

	{	gc = offscreen.getGraphics();

	

		gc.setFont(font);

		

		//draw and fill the category rectangle

                gc.setColor(catbgcolor[i]); 

		gc.fillRect(2, 2, catwidth - 4, height- 4);

		

		//draw the category string

                gc.setColor(cattextcolor[i]); 

		gc.drawString(cattext[i], catx[i], y);

	 

		//draw and fill the message rectangle

		gc.setColor(msgbgcolor);

		gc.fillRect(catwidth - 2, 2, width-4, height-4);

		

		//draw the message text centered at msgx

      gc.setColor(tempColor);

		gc.drawString(msgtext[i], msgx[i], y);

		

		//paint the black rectangle

		gc.setColor(Color.black); 

		gc.drawRect(0,0,width -1, height - 1);

		

		//paint the white rectangle

		gc.setColor(Color.white); 

		gc.drawRect(1,1,width -3, height -3);

		//gc.drawRect(2,2, width -5, height -5);

		

		g.drawImage(offscreen, 0, 0, null);

		gc.dispose();

	}



	public boolean mouseUp( Event evt, int x , int y ) 

	{	try {	URL thingee = new URL(textURL[i]);

				getAppletContext().showDocument(thingee);

			}

		catch (MalformedURLException e)

		{	System.out.println("hey I caught it!!!"); 

		}

		return true;

	}



	public boolean mouseEnter (Event evt, int x, int y) 

   { 

      tempColor = linkColor;

		update(g);

		return true;

	}



	public boolean mouseExit (Event evt, int x, int y) 

   {  tempColor =  msgtextcolor;

		update(g);

		return true;

	}

}

