Please send questions to
st10@humboldt.edu .
/**
* PaintPanel
*
* Used by TryJFramePainting; an extension of JPanel that expects
* a rectangle width when it is instantiated, and then draws
* a blue rectangle with that width along with a red message.
*
* setNewWidth() causes the panel to be repainted with the new
* width specified.
*
* whoops --- trusts its caller too much! Does *not* check if
* the integer value passed is in the 10-400 range! BEWARE.
* That should really be rectified, when this is made stand-alone...
*
* modified by: Sharon M. Tuttle
* last modified: 2-26-01
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public 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();
}
}