Java Reference
In-Depth Information
22 g = image.getGraphics();
23 g.setColor(Color.BLACK);
24
25 // encloses the image in a label inside a panel
26 JLabel label =
new
JLabel();
27 label.setIcon(
new
ImageIcon(image));
28 panel =
new
JPanel(
new
FlowLayout());
29 panel.setBackground(Color.WHITE);
30 panel.setPreferredSize(
new
Dimension(width, height));
31 panel.add(label);
32
33 // the status bar that shows the mouse position
34 statusBar =
new
JLabel(" ");
35
36 // attaches listener to observe mouse movement
37 panel.addMouseListener(
this
);
38 panel.addMouseMotionListener(
this
);
39
40 // sets up the JFrame
41 frame =
new
JFrame("Drawing Panel");
42 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43 frame.setResizable(
false
);
44 frame.setLayout(
new
BorderLayout());
45 frame.add(panel, BorderLayout.CENTER);
46 frame.add(statusBar, BorderLayout.SOUTH);
47 frame.pack();
48 frame.setVisible(
true
);
49
50 // starts a repaint timer to refresh the screen
51 Timer timer =
new
Timer(250,
this
);
52 timer.start();
53 }
54
55 // obtains the Graphics object to draw on the panel
56
public
Graphics getGraphics() {
57
return
g;
58 }
59
60 // sets the background color of the drawing panel
61
public void
setBackground(Color c) {
62 panel.setBackground(c);
63 }
64
65 // shows or hides the drawing panel on the screen
Search WWH ::
Custom Search