setOpaque(true);
size = new Dimension(0,0);
objects = new Vector();
//Set up the instructions.
JLabel instructionsLeft = new JLabel(
"Click left mouse button to place a circle.");
JLabel instructionsRight = new JLabel(
"Click right mouse button to clear drawing area.");
JPanel instructionPanel = new JPanel(new GridLayout(0,1));
instructionPanel.add(instructionsLeft);
instructionPanel.add(instructionsRight);
//Set up the drawing area.
drawingArea = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle rect;
for (int i = 0; i < objects.size(); i++) {
rect = (Rectangle)objects.elementAt(i);
g.setColor(colors[(i % color_n)]);
g.fillOval(rect.x, rect.y, rect.width, rect.height);
}
}
};
drawingArea.setBackground(Color.white);
drawingArea.addMouseListener(new MyMouseListener());
//Put the drawing area in a scroll pane.
JScrollPane scroller = new JScrollPane(drawingArea);
scroller.setPreferredSize(new Dimension(200,200));
//Layout this demo.
setLayout(new BorderLayout());
add(instructionPanel, BorderLayout.NORTH);
add(scroller, BorderLayout.CENTER);