//======================================================================//	PROJECT:			_programming.java_//	FILE:				Borders.java//	PURPOSE:			Chapter 2 lablet, new version//	VERSION:			1.1//	TARGET:				Java v1.2//	UPDATE HISTORY:		1.1		1/11/99		added Clicker class//						1.0		9/3/98		//======================================================================//------------------------------- NOTES --------------------------------/*	We're using some of the AWT graphics routines to draw borders of	different sizes in response to mouse clicks.  There's some fancy	(and potentially confusing) stuff going on in the Clicker	class, but we don't want you to get bogged down in the details.	Instead, just concentrate on the drawing we do in this applet	and don't worry about how Clicker works.*///------------------------------ IMPORTS -------------------------------/*	Notice that we're importing the same packages we did in the	Chapter 1 lablet.*/import java.applet.*;import java.awt.*;import java.awt.event.*;//=========================== Borders CLASS =============================/** * The Borders applet is very similar to the Colors applet of Chapter 1. * However, instead of changing the background and text color in response * to button clicks, this applet responds to button clicks by growing or * shrinking a decorative border drawn directly on the applet frame. */public class Borders extends Applet{	//------------------------------ DATA ------------------------------		private	int		inset = 5;			// amount of border inset, in pixels	private	Clicker	resizeBttn;			// shrinks or grows the border		//---------------------------- METHODS -----------------------------		/**	 * The only thing we need to do to start this applet is	 * make a new button and add it in.	 */	public void init()	{		resizeBttn = new Clicker(this, 50, 5);		add(resizeBttn);	}		/**	 * Redraw the border, using the applet's dimension and the	 * inset to tell us how to draw the rectangles.  As far as this	 * applet is concerned, this is the main method of interest (since	 * we hid the other important one down in the Clicker class). 	 */	public void paint(Graphics g) 	{		// Find out how big the applet is, and use that to compute the		// (x, y) anchor and (w, h) extent of the drawable area.		// NOTE: We're using local variables here, since appletSize,		// x, y, w, and h are only used in this method, and nowhere else.		Dimension appletSize = getSize();		int	x = 0,			y = 30,			w = appletSize.width - x,			h = appletSize.height - y;					// Draw the outer red rectangle (always the same size).			g.setColor(Color.red);		g.fillRect(x, y, w, h);				// Draw the black edge lines, inset as necessary.		g.setColor(Color.black);		g.drawRect(x + inset, y + inset, w - 2 * inset, h - 2 * inset);				// Draw the inner border, just inside the black lines		g.setColor(Color.white);		g.fillRect(x + inset + 1, y + inset + 1, w - 2 * inset - 1, h - 2 * inset - 1);				// Finally, draw the internal region in a color of our own.		Color fillColor = new Color(255, 204, 204);		g.setColor(fillColor);		g.fillRect(x + inset + 6, y + inset + 6, w - 2 * inset - 12, h - 2 * inset - 12);		}		/**	 * Change the value of inset and redraw the applet to reflect 	 * the new inset size.	 */	public void setInset(int numPixels)	{		inset = numPixels;			repaint();			// This forces paint() to be called.	}		/**	 * Allow the outside world (the Clicker object, in this case)	 * to inspect the value of inset.	 */	public int getInset()	{		return inset;	}	}//=========================== Clicker CLASS =============================/** * This class is an extension of an AWT Button.  Along with all * the things an ordinary button can do, a Clicker object monitors when * it is clicked and tells the applet that owns it to change its "inset" * variable appropriately, by calling the Borders method setInset(). * * NOTE: This is an overly complicated way of dealing with button clicks, * but we wanted to extract the details of event handling (which we're * not ready to talk about just yet) by sticking all the confusing bits in * this separate class. */class Clicker extends Button implements ActionListener{	//------------------------------ DATA ------------------------------		private Borders	owner;				// the parent applet	private boolean shrinking = true;	// shrinking or growing the border?	private int		maxInset,			// the largest allowable inset					minInset;			// the smallest allowable inset		//---------------------------- METHODS -----------------------------		/**	 * This is the recipe for making a new Clicker object.  This so-called	 * "constructor" gets called when Borders says "new Clicker(...)"	 */	public Clicker(Borders theApplet, int max, int min)	{		super("Smaller");			// Construct a regular Button, and then....		owner = theApplet;			// Record the owner of this object.		maxInset = max;				// Record the maximum inset value.		minInset = min;				// Record the minimum inset value.		addActionListener(this);	// Register this button as an event source.	}		/**	 * Respond to a click on ourself.  This method is called whenever the	 * user clicks us.	 */	public void actionPerformed(ActionEvent e) 	{		int inset = owner.getInset();	// Find out how big the applet's inset is.				//----- Change the inset size by +5 or -5 pixels.				if (shrinking)			// If we're shrinking the frame, increase inset		{			inset += 5;		}		else					// otherwise, decrease the inset.		{			inset -= 5;;		}		owner.setInset(inset);	// Finally, tell the applet to change its inset.				//----- If we're now at max or min inset, take appropriate action.				if (inset >= maxInset)		// If we're now at max inset...		{			setLabel("Larger!");	// change our label,			shrinking = false;		// and change direction flag.		}		else if (inset <= minInset)	// If we're now at min inset....		{			setLabel("Smaller!");			shrinking = true;		}	}}		