Java Reference
In-Depth Information
ch02/rectangles/RectangleViewer.java
1 import javax.swing.JFrame;
2
3 public class RectangleViewer
4 {
5 public static void main(String[] args)
6 {
7 JFrame frame = new JFrame();
8 frame.setSize( 300 , 400 );
9 frame.setTitle( ÐTwo rectanglesÑ );
10 frame.setDefaultCloseOperation(JFrame.EXIT
11
12 RectangleComponent component = new
RectangleComponent();
13 frame.add(component);
14
15 frame.setVisible( true );
16 }
17 }
Note that the rectangle drawing program consists of two classes:
ȗ
The RectangleComponent class, whose paintComponent method
produces the drawing
ȗ
The RectangleViewer class, whose main method constructs a frame and a
RectangleComponent , adds the component to the frame, and makes the
frame visible
62
63
S ELF C HECK
29. How do you modify the program to draw two squares?
30. How do you modify the program to draw one rectangle and one square?
31. What happens if you call g.draw(box) instead of g2.draw(box) ?
Search WWH ::




Custom Search