Please send questions to st10@humboldt.edu .
/**
 * TryJAppletPainting2
 * 
 * modification of TryJAppletPainting, extending that simple example
 * of how to accomplish painting on a JApplet. Now, we demonstrate
 * how repainting is accomplished, repainting the rectangle using
 * the width entered for it in a JTextField, resizing it whenever a
 * new width is entered for it.
 *
 * RECOMMENDED SIZE: 400 x 500
 *
 * modified by: Sharon M. Tuttle
 * last modified: 2-18-01
 **/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TryJAppletPainting2 extends JApplet implements ActionListener
{
	// I do not paint on the applet itself, nor even on its
	// content pane --- I paint on a "local" extension of JPanel
	PaintPanel 		paintOnMe;
	
	Container		myContentPane;

	// in the South, I'll now have a panel with a labelled
	// textfield, awaiting a rectangle width
	JLabel			rectWidthLabel;
	JTextField		rectWidthField;
	JPanel			rectWidthPanel;
	
	public void init()
	{
		myContentPane = getContentPane();

		// instantiate an instance of my "local" panel for painting on
		// with width 50
		// (yes, I've added a parameter to my constructor for my
		// local extension of JPanel...)
		paintOnMe = new PaintPanel(50);

		myContentPane.add(paintOnMe, "Center");		

		// set up the rectWidthLabel and rectWidthField on
		// a rectWidthPanel
		rectWidthPanel = new JPanel();
		rectWidthPanel.setLayout(new FlowLayout());

		rectWidthLabel = new JLabel("Enter integer width (10-400): ");
		rectWidthPanel.add(rectWidthLabel);

		rectWidthField = new JTextField(5);
		rectWidthField.addActionListener(this);		
		rectWidthPanel.add(rectWidthField);

		myContentPane.add(rectWidthPanel, "South");
	}

	public void actionPerformed(ActionEvent e)
	{
		int	newWidth;

		// if value entered in the rectWidthField is an integer in 
		// the right range, paint the new-width rectange. Else
		// clear out the rectWidthField to await another try
		try
		{
			newWidth = Integer.parseInt(rectWidthField.getText());
			if ((newWidth >= 10) && (newWidth <= 400))
			{
				// notice that this is a method of my local
				// extension of JPanel
				paintOnMe.setNewWidth(newWidth);
			}
			else
			{
				rectWidthField.setText("");
			}
		}
		catch (NumberFormatException exc)
		{
			rectWidthField.setText("");
		}
	}
}

// this extension of JPanel is painted on...
class PaintPanel extends JPanel
{
	private	int 	rectWidth;	// the desired width of the rectangle
								// to be painted

	// this constructor sets the width for the rectangle
	// given its integer parameter
	public PaintPanel(int width)
	{
		rectWidth = width;
	}
		
	// you want to implement paintComponent() here, not
	// paint()...
	public void paintComponent(Graphics g)
	{
		String		information;

		information = "I am a painted message";

		// NEED this! superclass of PaintPanel may need to do
		// some cleanup; this calls superclass's paintComponent()
		// method
		super.paintComponent(g);

		// paint a red message on the panel
		g.setFont(new Font("Serif", Font.ITALIC, 45));
		g.setColor(Color.red);
        	g.drawString(information, 10, 200);

		// paint a blue rectangle with width rectWidth on the panel
		g.setColor(Color.blue);

		// notice that rectangle now has width rectWidth...
		g.drawRect(50, 250, rectWidth, 100);
	}

	// repaint with the given value of width as the rectangle's 
	// new width
	public void setNewWidth(int newWidth)
	{
		rectWidth = newWidth;
		
		// this will force an execution of paintComponent()	
		repaint();
	}
}