Java Reference
In-Depth Information
9
10 g.setColor(Color.RED);
11 g.fillRect(20, 40, 70, 30);
12 g.setColor(Color.BLUE);
13 g.fillRect(60, 10, 20, 80);
14 }
15 }
We can now write a separate class for a GUI that incorporates this panel. In the
GUI class, we create a RectPanel object and add it to the frame. The rectangle will
appear on the screen with the two shapes drawn on top of it. Here's the example
client code that uses our RectPanel :
1 // Demonstrates the RectPanel class by placing one into a GUI.
2
3 import java.awt.*;
4 import javax.swing.*;
5
6 public class UseRectPanel {
7 public static void main(String[] args) {
8 JFrame frame = new JFrame();
9 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10 frame.setSize(200, 200);
11 frame.setTitle("A panel with rectangles");
12
13 RectPanel panel = new RectPanel();
14 panel.setBackground(Color.WHITE);
15 frame.add(panel);
16
17 frame.setVisible( true );
18 }
19 }
The program produces the following graphical output:
Search WWH ::




Custom Search