Java Reference
In-Depth Information
You might wonder why you can't just use a DrawingPanel rather than going to
all this trouble. There are several reasons. One is that DrawingPanel isn't actually a
standard part of Java, so you can't rely on it outside a classroom setting. Also,
although it's a nice tool for simple drawing, you can't make a DrawingPanel part of
a larger GUI with other components. The following modified code for the previous
client program achieves a mixture of components that would be impossible to repli-
cate with a DrawingPanel :
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setTitle("A panel with rectangles");
frame.setLayout(new BorderLayout());
JPanel north = new JPanel(new FlowLayout());
north.add(new JLabel("Type your name:"));
north.add(new JTextField(10));
frame.add(north, BorderLayout.NORTH);
frame.add(new JButton("Submit"), BorderLayout.SOUTH);
RectPanel panel = new RectPanel();
panel.setBackground(Color.WHITE);
frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
The program produces the following graphical output:
For a more complete description of two-dimensional graphical methods and
objects, see Supplement 3G's discussion of the Graphics object.
Animation with Timers
A panel doesn't have to show a simple static drawing. You can use an object called a
timer, implemented by an object of the Timer class, to animate a panel to show a
 
Search WWH ::




Custom Search