FileDocCategorySizeDatePackage
ScrollDemo2.javaAPI DocExample4042Tue Dec 12 18:59:20 GMT 2000None

ScrollDemo2

public class ScrollDemo2 extends JPanel

Fields Summary
private Dimension
size
private Vector
objects
private final Color[]
colors
private final int
color_n
JPanel
drawingArea
Constructors Summary
public ScrollDemo2()


      
        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);
    
Methods Summary
public static voidmain(java.lang.String[] args)

        JFrame frame = new JFrame("ScrollDemo2");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });

        frame.setContentPane(new ScrollDemo2());
        frame.pack();
        frame.setVisible(true);