Sketcher.java

Download Sketcher.java

 1: // This program creates a blank window and shows how to listen
 2: // and use mouse events.  This program allows users to draw
 3: // lines by clicking with the mouse.  A double-click ends
 4: // the current series of lines.  A right-click clears the window.
 5: // (In addition to the test for a right-click shown, you can also
 6: // test a MouseEvent e for shift-click, alt-click, ctl-click, etc.)
 7: 
 8: // Written 2003 by Wayne Pollock, Tampa FL USA.
 9: 
10: import java.awt.*;
11: import java.awt.event.*;
12: 
13: public class Sketcher extends Frame implements MouseListener
14: {  private int lastX, lastY, x, y;  // The coordinates of mouse clicks.
15:    private boolean clearWindow, newSeriesOfLines;
16: 
17:    public static void main ( String [] args )
18:    {  Sketcher win = new Sketcher();
19: 
20:       // Window closing handler:
21:       win.addWindowListener( new WindowAdapter()
22:          {  public void windowClosing ( WindowEvent we )
23:             {  System.exit( 0 );  }
24:          }
25:       );
26:    }
27: 
28:    Sketcher ()
29:    {  setTitle( "Sketcher" );
30:       setSize( 350, 250 );
31:       clearWindow = true;
32:       newSeriesOfLines = true;
33: 
34:       // Hook up event handling:
35:       addMouseListener( this );
36:       setVisible( true );
37:    }
38: 
39:    public void mouseReleased( MouseEvent e )
40:    {  // Check for a right-click:
41:       if ( e.getButton() == MouseEvent.BUTTON3 )
42:       {  clearWindow = newSeriesOfLines = true;
43:          System.err.println( "Right-Click!" );
44:          repaint();
45: 
46:       } else if ( e.getClickCount() == 1 )  // A single click, or
47:       {  x = e.getX();                      // 1st of a dbl click.
48:          y = e.getY();
49:          System.err.println( "Single-Click at (" + x + ", " + y + ")" );
50:          repaint();
51: 
52:       } else  // A double (or more) click!
53:       {  newSeriesOfLines = true;
54:          System.err.println( "Double-Click!" );
55:       }
56:    }
57: 
58:    public void mouseEntered  ( MouseEvent e ) { }
59: 
60:    public void mouseExited   ( MouseEvent e ) { }
61: 
62:    public void mousePressed  ( MouseEvent e ) { }
63: 
64:    public void mouseClicked ( MouseEvent e ) { }
65: 
66:    // Over-ride update to prevent "flicker":
67:    public void update( Graphics g )
68:    {  paint( g );
69:    }
70: 
71:    public void paint ( Graphics g )
72:    {
73:       System.err.println( "In paint(): x=" + x + ", y=" + y +
74:                           ", lastX=" + lastX + ", lastY=" + lastY );
75:       if ( clearWindow )
76:       {  Dimension d = this.getSize();
77:          g.clearRect( 0, 0, d.width, d.height );
78:          int topInset = getInsets().top;
79:          g.drawString( "Double-click to mark end of series, " +
80:                        "right-click to erase.", 15, topInset + 15 );
81:          clearWindow = false;
82:          return;
83:       }
84: 
85:       if ( ! newSeriesOfLines )
86:          g.drawLine( lastX, lastY, x, y );
87:        else
88:          newSeriesOfLines = false;
89: 
90:       lastX = x;
91:       lastY = y;
92:    }
93: }