Java Reference
In-Depth Information
8 */
9 public class RectangleComponent extends
JComponent
10 {
11 public void paintComponent(Graphics g)
12 {
13 // RecoverGraphics2D
14 Graphics2D g2 = (Graphics2D) g;
15
16 // Construct a rectangle and draw it
17 Rectangle box = new Rectangle( 5 , 10 ,
20 , 30 );
18 g2.draw(box);
19
20 // Move rectangle 15 units to the right and 25 units
down
21 box.translate( 15 , 25 );
22
23 // Draw moved rectangle
24 g2.draw(box);
25 }
26 }
61
62
In order to see the drawing, one task remains. You need to display the frame into
which you added a component object. Follow these steps:
1. Construct a frame as described in the preceding section.
2. Construct an object of your component class:
RectangleComponent component = new
RectangleComponent();
3. Add the component to the frame:
frame.add(component);
4. Make the frame visible, as described in the preceding section.
The following listing shows the complete process.
Search WWH ::




Custom Search