# Java Notes

# Example - Scribble

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;
    /** ScribbleGUI.java
        @author Fred Swartz
        @version 2003-06 (Pavlov Czecho) */
    class ScribbleGUI extends JPanel{
        private int last_x, last_y;   // last x,y position of the mouse
        private ArrayList    _points = new ArrayList(1000);
        private DrawingPanel _drawing = new DrawingPanel();

        public ScribbleGUI() {  //================================== constructor
            // Create and add a button to the Panel
            JButton b = new JButton("Clear");
            b.addActionListener(new ClearListener());

            //--- Layout components
            this.setLayout(new BorderLayout());
            this.add(b, BorderLayout.NORTH);  // add the button to the applet
            this.add(_drawing, BorderLayout.CENTER);
        }// end init

        ////////////////////////////////////////////// inner class ClearListener
        private class ClearListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                _points.clear();
                _drawing.repaint();
            }//end actionPerformed
        }//end class ClearListener

        //////////////////////////////////////////////// inner class DrawingPanel
        private class DrawingPanel extends JPanel implements MouseListener,
                                                     MouseMotionListener {
            //====================================================== constructor
            public DrawingPanel() {
                setPreferredSize(new Dimension(300, 300));
                setBackground(Color.white);
                addMouseMotionListener(this);
                addMouseListener(this);
            }//end constructor

            public void clear() {
                _points.clear();
                this.repaint();
            }//end clear

            //==================================================x paintComponent
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                for (int i=0; i < _points.size()-1; i++) {
                    Point previous = (Point)_points.get(i);
                    Point next     = (Point)_points.get(i+1);
                    g.drawLine(previous.x, previous.y, next.x, next.y);
                }
            }//end paintComponent

            public void mousePressed(MouseEvent e) {
                _points.add(e.getPoint());
                repaint();
            }//end mousePressed

            public void mouseDragged(MouseEvent e) {
                _points.add(e.getPoint());
                repaint();
            }//end mouseDragged

            //--- Ignore these events
            public void mouseReleased(MouseEvent e) { }
            public void mouseClicked (MouseEvent e) { }
            public void mouseEntered (MouseEvent e) { }
            public void mouseExited  (MouseEvent e) { }
            public void mouseMoved   (MouseEvent e) { }
        }//end class DrawingPanel
    }//end class ScribbleGUI