Java Reference
In-Depth Information
9 private JFrame frame; // overall window frame
10 private JPanel panel; // drawing surface
11 private Graphics g; // drawing pen
12
13 // constructs a drawing panel of given size
14 public DrawingPanel( int width, int height) {
15 // sets up the empty image onto which we will draw
16 BufferedImage image = new BufferedImage(width, height,
17 BufferedImage.TYPE_INT_ARGB);
18 g = image.getGraphics();
19 g.setColor(Color.BLACK);
20
21 // enclose the image in a label inside a panel
22 JLabel label = new JLabel();
23 label.setIcon( new ImageIcon(image));
24 panel = new JPanel( new FlowLayout());
25 panel.setBackground(Color.WHITE);
26 panel.setPreferredSize( new Dimension(width, height));
27 panel.add(label);
28
29 // set up the JFrame
30 frame = new JFrame("Drawing Panel");
31 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32 frame.setResizable( false );
33 frame.add(panel);
34 frame.pack();
35 frame.setVisible( true );
36 }
37
38 // obtains the Graphics object to draw on the panel
39 public Graphics getGraphics() {
40 return g;
41 }
42
43 // sets the background color of the drawing panel
44 public void setBackground(Color c) {
45 panel.setBackground(c);
46 }
47
48 // shows or hides the drawing panel on the screen
49 public void setVisible( boolean visible) {
50 frame.setVisible(visible);
51 }
52 }
Search WWH ::




Custom Search