Please send questions to
st10@humboldt.edu .
/**
* TryJFramePainting
*
* Try to redo TryJAppletPainting2 so that it is for a JFrame
* instead of for a JApplet. Can we get the repaint to work, also,
* so that the rectangle is drawn with a new width whenever a new
* "legal" width is entered into the textfield? Yes;
*
* Other slight twist here: PaintPanel is in a separate class
* within the same directory/BlueJ project.
*
* modified by: Sharon M. Tuttle
* last modified: 2-26-01
**/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class TryJFramePainting
{
public static void main(String args[])
{
// subclass of JFrame for this application
TryJFramePaintingFrame mainFrame;
mainFrame = new TryJFramePaintingFrame();
mainFrame.setTitle("\"Painting\" on a JFrame");
mainFrame.show();
}
}
// this class within TryJFramePainting sets up a custom extension
// of JFrame for this application, one that has a painted JPanel
// upon it
class TryJFramePaintingFrame extends JFrame implements ActionListener
{
// I do not paint on the frame itself, nor even on its
// content pane --- I paint on a "local" extension of JPanel
private PaintPanel paintOnMe;
private Container myContentPane;
// in the South, I'll now have a panel with a labelled
// textfield, awaiting a rectangle width
private JLabel rectWidthLabel;
private JTextField rectWidthField;
private JPanel rectWidthPanel;
public TryJFramePaintingFrame()
{
myContentPane = getContentPane();
// instantiate an instance of my "local" panel for painting on
// with initial blue rectangle width of 100. Let's give it a
// border, too --- can I do that, here? Or not?
paintOnMe = new PaintPanel(100);
paintOnMe.setBorder(new TitledBorder(
new EtchedBorder(), "Painted Area"));
myContentPane.add(paintOnMe, "Center");
// set up window-closing handling for this frame
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(500, 500);
// set up the rectWidthLabel and rectWidthField on
// a rectWidthPanel. Let's give the rectWidthPanel a border,
// too.
rectWidthPanel = new JPanel();
rectWidthPanel.setLayout(new FlowLayout());
rectWidthPanel.setBorder(new TitledBorder(
new EtchedBorder(), "Control Width of Rectangle"));
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("");
}
}
}