//======================================================================//	PROJECT:			_programming.java_//	FILE:				Colors.java//	PURPOSE:			Chapter 1 lablet, new version//	VERSION:			1.1//	TARGET:				Java v1.1//	UPDATE HISTORY:		1.1		8/27/98		changed to v1.1 compliance//						1.0		4/21/96		the version in _p.j_//======================================================================//------------------------------- NOTES --------------------------------/*	This example demonstrates some of the features of a Java applet.  We	obviously don't expect you to understand all (or even most) of the	details--think of this as a prototype of what a typical applet looks	like.  For exmple, this text is a comment--since we've enclosed it	in a "slash-star...star-slash" pair, the compiler will completely	ignore it.  There are three different kinds of comments in Java,	all of which are used in the first 40 lines of this file.*///------------------------------ IMPORTS -------------------------------/*	A lot of what a Java program does depends on packages of classes	that the nice people at Sun have written for us.  If we're using	any of these classes we have to "import" their packages so the 	compiler knows what words like "Applet" and "Button" mean.*/import java.applet.*;import java.awt.*;import java.awt.event.*;//=========================== Colors CLASS =============================/** * The Colors class is an extension of the Applet class, meaning that * it has all the properties of the Applet class, along with whatever * data and methods we've added.  The "implements ActionListener" clause * means that we've implemented a method named actionPerformed(), so * that our applet will respond to actions like pressing buttons. */public class Colors extends Applet implements ActionListener{	//------------------------------ DATA ------------------------------		int			bgCode = 0,				txtCode = 0;	Button		backButton,				textButton;	Font 		f = new Font("Helvetica",Font.BOLD,18);		//---------------------------- METHODS -----------------------------		/**	 * This function is called into action when the applet first starts 	 * running and is used to lay out all the objects and initialize them 	 * if needed. Almost all applets have an init() method.   Here, we	 * generate two new buttons, add them to our applet, and register the	 * fact that "this" class will be the listener for button clicks.	 *    Notice that here and throughout, we're calling a lot of functions	 * like add() and addActionListener() that are defined elsewhere	 * (which is why we needed the "import" statements above).	 */	public void init() 	{		backButton = new Button("Background Color");		add(backButton);		backButton.addActionListener(this);				textButton = new Button("Text Color");		add(textButton);		textButton.addActionListener(this);	}		/**	 * Respond to any click on a button.  We've registered the applet	 * as an ActionListener for both buttons, so whenever an action takes 	 * place this method will be invoked.  What it does is find out which	 * of the two buttons triggered the event and then execute the 	 * appropriate code for the button.	 */	public void actionPerformed(ActionEvent e) 	{		Object source = e.getSource();	// Who generated the event?		if (source == backButton)		// if backButton did, do this...		{			bgCode = ++bgCode % 4;			switch (bgCode)			{				case 0: setBackground(Color.cyan);		break;				case 1: setBackground(Color.orange);	break;				case 2: setBackground(Color.red);		break;				case 3: setBackground(Color.green);		break;			}			}		else if (source == textButton)	// if textButton did, do this...		{			txtCode = ++txtCode % 3;		}			repaint();	// Force a call to paint, so the new colors show up.	}		/**	 * Perform any graphic updating that's needed.  Buttons already "know" 	 * how to draw themselves, for example, but the system has no way of 	 * knowing that we intend to change the background and text colors, 	 * so we have to provide that functionality ourselves. 	 */	public void paint(Graphics g) 	{			switch (txtCode)		{			case 0: g.setColor(Color.blue);		break;			case 1: g.setColor(Color.magenta);	break;			case 2: g.setColor(Color.black);	break;		}				g.setFont(f);		g.drawString("Goodbye world!  Hello Java!", 30, 120);		}	}		