SOURCE CODE
Home

 

This is my source code for Assignment5
(Text Scroller Applet with event processing)      
This applet illustrates animation, advanced graphics, and event 
processing in JAVA. The applet scrolls text just like in the closing
credits in a movie. Let's dig deeper to find out how the program works!!!.
                           
Source Code:
/*********************************************************************
 * Project5.JAVA 10/08/99											   	    
 * 																			    
 * This applet illustrates the use of advanced graphics, animation,   
 * and event processing.  The animation is running in its own thread.  
 * It scrolls text on the applet and wraps the text around the applet.  
 * It also shows the use of double buffering to reduce flickering in 													    
 * the animation.  As well as processing mouse events and key events																			    
 * to interact with the user.
 * 
 * @author Allan V. Oliva												    
 ********************************************************************/
 
import java.awt.*;
import java.awt.event.*;
import java.applet.AudioClip;

/*********************************************************************
 * Project5 class
 *
 * This class is the main driver of the applet it initializes the text
 * to be displayed on the applet screen as well as the applet's background
 * color.  Then a thread is created to run the animation, which scrolls
 * the text.  MouseListener and KeyListener are registered with the applet.
 * These listeners tract to see if an event has occured.
 *
 *********************************************************************/
public class Project5 extends java.applet.Applet implements 
MouseListener, KeyListener, Runnable
{
	static final int size = 16;		//Number of Lines of "text"
	String [] Lines = new String[size];	//Lines array holds the "text"
	int [] yPos = new int[size];		//yPos holds each "texts'" position
	String [] Messages = new String[9]; 	//Holds the alert messages
	
	//Font object created to use in drawing text
	Font f = new Font("Sanserif",Font.BOLD,12); 
	Font p = new Font("Serif",Font.BOLD,20);

	Thread runner;		//Workload of animation done by this thread
		
	Image offscreenImg;	//Image object for double buffering
	Graphics offscreen;	//Graphics object for double buffering
	
	int yMax;		//Position after last line of text
	int spacing;		//Spacing between lines of text
	int speed = 70;		//Speed of animation
	int messageNo=0;	//Tells which message to display      
	boolean alertmessage=false; 	//Flag that alert message exist
	boolean scrollOn=true;		//Flag if scroll is on/off
	boolean changebackground=false; //Flag to switch background color
	boolean fast = false;		//Flag if fast scroll on/off
	boolean sound = false;		//Flag if sound on/off
	
	//Color of text & applets background
	Color background = new Color(0,0,0);
	Color foreground = new Color(255,255,255);
	Color background2 = new Color(0,0,255);

	AudioClip beep;		//Object for soundclip
	
	
       /*===================================================================
	* init method
	*
	* Initialize applet's background,the array of text, and the alert   
	* messages as well as the position of the text.  The offscreen surface
     	* is also initialized which will be used for double buffering. 
    	* 
	*===================================================================*/
	public void init()
	{
    		//Register Mouse Listener to applet
		addMouseListener(this);
		
		//Register Key Listener to applet
		addKeyListener(this);
	    		
		//Offscreen surface created which is size of applet
		//will be used for double buffering; eliminates flickering
		offscreenImg = createImage(getSize().width, getSize().height);
	    	offscreen = offscreenImg.getGraphics();
		 
        	spacing = 15;	//Set spacing
		 		 		 
	 	   //Initialize the lines of text
		Lines[0] = "Is it worth it? With prices for new";
	 	Lines[1] = "computers dropping faster than Al Gore's";
	    	Lines[2] = "approval ratings, buying refurbished isn't ";
	    	Lines[3] = "as attractive an option as it used to be.";
	    	Lines[4] = "";
	    	Lines[5] = "But it's still a good way to maximize";
	    	Lines[6] = "computing power for less...as long as you";
	    	Lines[7] = "don't mind doing a little comparison shopping.";
	    	Lines[8] = "";
	    	Lines[9] = "In fact, you can use the low prices on new";
	    	Lines[10]= "PCs to your advantage when you're haggling";
	    	Lines[11]= "with the dealer.  If it was good enough for";
	    	Lines[12]= "Thoreau, it should be good enough for you.";
	    	Lines[13]= "";
	    	Lines[14]= "";
	    	Lines[15]= "";
		
		//Initialize alert messages
		Messages[0]= "";
		Messages[1]= "Scrolling Suspended";
		Messages[2]= "Scrolling Resumed";
		Messages[3]= "Fast Scroll Enabled";
		Messages[4]= "Normal Scroll Enabled";
		Messages[5]= "Change Background";
		Messages[6]= "Original Background";
		Messages[7]= "Sound Enabled";
		Messages[8]= "Sound Off";
		
		//Position of first Line of text
	    	yPos[0] = getSize().height + spacing; 
		
		//Loop sets the position of other lines
		//of text based on first position
	    	for(int x=1;x<Lines.length;x++)
	    	{
			yPos[x] = yPos[x-1]+ spacing;
		} //End of for loop
		 
		//Position after the last line of text
		yMax = yPos[(Lines.length-1)] - getSize().height;
		
		//Sets buffer's background color 
		offscreen.setColor(background);
	    	offscreen.fillRect(0,0,getSize().width, getSize().height); 
	
	    	//Retrieve sound clip from audio folder
		beep = getAudioClip(getCodeBase(), "audio/spacemusic.au");
		
	}  //End of init method
	
	
       /*===================================================================
	* start method
	*
	* Checks to see if a thread has been created. If not it creates the
	* thread.  The thread's start method is called which calls another
	* method--the run() method.  This will start the thread.
     	*===================================================================*/
	public void start()
	{
		if (runner == null) 
		{
	    		//The new thread is running the applet object
			runner = new Thread(this);  
			runner.start();
		}  //End of if
	
	}  //End of start method	 
	
	
       /*===================================================================
	* stop method
	*
	* Checks to see if a thread exist. If there is a thread 
	* then it gives it the value null.  This will stop the thread.
	*===================================================================*/
	public void stop()
	{
		if (runner != null)
		{
			//Checks if sound exists and stops it.
			if (beep !=null) beep.stop();
			runner = null;
		}  //End of if
		
	}  //End of stop method
	
	
       /*===================================================================
	* run method
	*
	* Method that drives animation sequence.  A single animation frame is 
	* constructed in the while loop.  This method triggers update method    
	* and scrolls the text by updating the Y position.  If the line of
	* text scrolls off the visible area of the applet then make that
	* line of text go to the bottom of the applet screen.
	*===================================================================*/
	public void run()
	{
	    
		
		//Creates thread makes it current		 
		Thread thisThread = Thread.currentThread();
		
		
		//Loop creates frame of animation as long as runner is the current thread
		while (runner==thisThread)
		{
			
			repaint();  //Calls update method then eventually paint method
			
		      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			% 
			% Loop that scrolls the text by updating yPos.  Checks if the alert
			% message has been displayed.  If it has, continue animation, else 
			% remain at the current position. Also checks if the lines of text has
			% rolled off the applet screen; If it has, resets the position of the
			% line of text to print at bottom of applet screen.
			%
			%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
			for(int x=0;x<Lines.length;x++)
			{
				//Check if alert message has been displayed
				if ((scrollOn==true) && (alertmessage==false)) yPos[x]--;
				else
					yPos[x]= yPos[x];
				
				//Check if position of text off the applet screen
				if (yPos[x] < 0)      
				{					     
			  		yPos[x]=yMax;
		
			  	} //End of if  
			
			} //End of for loop
			  
			pause(speed);  //pauses 70 milliseconds
			         			
		}  //End of while loop
		
	}  //End of run method
    
	
       /*===================================================================
    	* pause method
	*
	* Controls how fast animation actually takes place
	*===================================================================*/
	void pause(int time)
	{
		try
		{
			Thread.sleep(time);	
		}
		catch (InterruptedException e) {}
	
	}  //End of pause method
	
	
	/*===================================================================
	 * paint method
	 *
	 * paint the lines of text to an offscreen image(buffer)
	 * then paints it to the applet screen
	 *===================================================================*/
	public void paint(Graphics g)
	{
				
		//Paints background color of buffer
		//Checks which background color needs to be painted
		if (changebackground==true)
		{
			offscreen.setColor(background2);
			offscreen.fillRect(0,0,size().width, size().height);
		
		} //End of if
		else
		{
			offscreen.setColor(background);
			offscreen.fillRect(0,0,size().width, size().height);
		} //End of else
		
				
		//Set color and font of text  
		offscreen.setColor(foreground);
		offscreen.setFont(f);
					
		//Loop that draws actual Lines of text at the specified Y position
		for (int x=0; x<Lines.length;x++)
		{	
			offscreen.drawString(Lines[x],15,yPos[x]);
				
		}  //End of for loop     
		
		//Draws image to applet
		g.drawImage(offscreenImg,0,0,this);				 
	
	}  //End of paint method
	
	
	/*===================================================================
	 * update method
	 *
	 * overrides update method, so it does not clear the screen
	 * clearing the screen will make animation flicker
	 *===================================================================*/
	public void update(Graphics g)
	{
		//Checks if alert message needs to be displayed
		//if yes then gets the message
		if (alertmessage==true) 
		{
			getmessage(g);
			
		}  //End of if
		
		paint(g);
		
	}  //End of update method
	
	/*===================================================================
	 * getmessage method
	 *
	 * Determines which alert message needs to be displayed on the applet.
	 * Displays the actual message on the applet screen.
	 *===================================================================*/
	public void getmessage(Graphics g)
	{
		//Find out which message to display from the message number
		switch (messageNo)
		{
			case 1:	//Display "Scrolling Suspended"
			{
				//Loop blinks alert message on applet screen 
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],50,getSize().height/2);
					pause(500);
					if (changebackground==true)
					{
						g.setColor(background2);
						g.drawString(Messages[messageNo],50,getSize().height/2);
					} //End of if
					else
					{
						g.setColor(background);
						g.drawString(Messages[messageNo],50,getSize().height/2);
					} //End of else
					pause(100);
					
				} //End of for loop
				alertmessage=false;
				return;		
			} //End of case
			
			case 2:	//Display "Scrolling Resumed"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],60,getSize().height/2);
					pause(500);
					if (changebackground==true)
					{
						g.setColor(background2);
						g.drawString(Messages[messageNo],60,getSize().height/2);
					} //End of if
					else
					{
						g.setColor(background);
						g.drawString(Messages[messageNo],60,getSize().height/2);
					} //End of else
					pause(100);
					
				} //End of for loop
				alertmessage=false;
				return;
			} //End of case	
			
			case 3:	//Display "Fast Scroll Enabled"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],60,getSize().height/2);
					pause(500);
					if (changebackground==true)
					{
						g.setColor(background2);
						g.drawString(Messages[messageNo],60,getSize().height/2);
					} //End of if
					else
					{
						g.setColor(background);
					    g.drawString(Messages[messageNo],60,getSize().height/2);
					} //End of else
					pause(100);
					
				} //End of for loop
				alertmessage=false;
				return;
			} //End of case	
				
			case 4:	//Display "Slow Scroll Enabled"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],60,getSize().height/2);
					pause(500);
					if (changebackground==true)
					{
						g.setColor(background2);
						g.drawString(Messages[messageNo],60,getSize().height/2);
					} //End of if
					else
					{
						g.setColor(background);
						g.drawString(Messages[messageNo],60,getSize().height/2);
					} //End of else
					pause(100);
					
				} //End of for loop
				alertmessage=false;
				return;
			} //End of case		
			
			case 5:	//Display "Change Background"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],55,getSize().height/2);
					pause(500);
					g.setColor(Color.black);
					g.drawString(Messages[messageNo],55,getSize().height/2);
					pause(100);
				} //End of for loop
				alertmessage=false;
				return;
			} //End of case		
		
			case 6:	//Display "Original Background"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],50,getSize().height/2);
					pause(500);
					g.setColor(Color.blue);
					g.drawString(Messages[messageNo],50,getSize().height/2);
					pause(100);
				} //End of for loop
				alertmessage=false;
				return;
			} //End of case		
		
			case 7:	//Display "Sound Enabled"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],80,getSize().height/2);
					pause(500);
					if (changebackground==true)
					{
						g.setColor(background2);
						g.drawString(Messages[messageNo],80,getSize().height/2);
					} //End of if
					else
					{
						g.setColor(background);
						g.drawString(Messages[messageNo],80,getSize().height/2);
					} //End of else
					pause(100);
							
				} //End of for loop
				alertmessage=false;
				return;
			} //End of case		
			
			case 8:	//Display "Sound Off"
			{
				//Loop blinks alert message on applet screen
				for (int x=0; x<3; x++)
				{
					g.setColor(Color.red);
					g.setFont(p);
					g.drawString(Messages[messageNo],90,getSize().height/2);
					pause(500);
					if (changebackground==true)
					{
						g.setColor(background2);
						g.drawString(Messages[messageNo],90,getSize().height/2);
					
					} //End of if
					else
					{
						g.setColor(background);
						g.drawString(Messages[messageNo],90,getSize().height/2);
					
					} //End of else
					pause(100);
							
				} //End of for 
				alertmessage=false;
				return;
			
			} //End of case	
			
		} //End of switch	
		
	} //End of getmessage method	
    
	/*===================================================================
	 * destroy method
	 *
	 * kill offscreen image
	 *===================================================================*/
	public void destroy()
	{
		offscreen.dispose();
			
	} //End of destroy method

	
	/************************Mouse Events************************************/
	/*===================================================================
	 * mouseClicked method
	 *
	 * Determines if left mouse button has been clicked then checks if  
	 * text is not scrolling fast, if it is not make text scroll fast
	 * otherwise scroll slow.
	 *===================================================================*
	public void mouseClicked(MouseEvent e) 
	{
		//Determines if the left mouse button has been "clicked"
		if (((e.getModifiers() & (1 << 4)) >> 4) == 1)
		{
			if(fast==false)
			{
				speed=20;
				fast=true;
				alertmessage=true;
				messageNo=3;
				
			} //End of if
			else		
			{
				speed=110;
				alertmessage=true;
				fast=false;
				messageNo=4;
			} //End of else
		} //End of if		
	} //End of mouseClicked method
		
 
	/*===================================================================
	 * mousePressed method
	 *
	 * Determines if right mouse button has been 
	 * pressed if it has, then suspend the animation.
	 *===================================================================*
	public void mousePressed(MouseEvent e)
	{
		//Determines if right mouse button is being pressed
		if (((e.getModifiers() & (1 << 2)) >> 2) == 1)
		{
			alertmessage=true;
			messageNo=1;
			scrollOn=false;
		} //End of if
					
	} //End of mousePressed method
		
	
	/*===================================================================
	 * mouseReleased method
	 *
	 * Determines if right mouse button has been 
	 * released if it has, then resume the animation.
	 *===================================================================*
	public void mouseReleased(MouseEvent e) 
	{
		//Determines if right mouse button has been released
		if (((e.getModifiers() & (1 << 2)) >> 2) == 1) 
		{
			alertmessage=true;
			scrollOn=true;
			messageNo=2;
	
		} //End of if
	} //End of mouseReleased method

	
	/*===================================================================
	 * mouseEntered method
	 *
	 * Determines if mouse has entered the applet screen 
	 * if it has, then change the background color of applet.
	 *===================================================================*
	public void mouseEntered(MouseEvent e) 
	{
		changebackground = true;
		alertmessage=true;
		messageNo=5;
		
	} //End of mouseEntered method


	/*===================================================================
	 * mouseExited method
	 *
	 * Determines if mouse has exited the applet screen if it has then
	 * change the background color of applet to original color of applet.
	 *===================================================================*
	public void mouseExited(MouseEvent e) 
	{
		changebackground =false;
		alertmessage=true;
		messageNo=6;
	} //End of mouseExited method
	
	/**********************End of Mouse Events******************************/
	
		
	/////////////////////////Keyboard Events////////////////////////////////
	/*===================================================================