Mouse Test

Download: jar file

This is the MouseTest program from the textbook, p 349-357, with some modifications.

Demonstration

To run as an applet, include the following tag in an HTML file:

  <applet code="MouseTest.class" archive="MouseTest.jar" width=300 height=200></applet>

MouseTest.java


001: import java.awt.*;
002: import java.awt.event.*;
003: import java.util.*;
004: import java.awt.geom.*;
005: import javax.swing.*;
006: 
007: public class MouseTest extends JApplet
008: {
009:     public void init()
010:     {
011:         EventQueue.invokeLater(new Runnable() {
012:             public void run()
013:             {
014:                 MousePanel panel = new MousePanel();
015:                 add(panel);
016:             }
017:         });
018:     }
019:    public static void main(String[] args)
020:    {
021:       EventQueue.invokeLater(new mainThread());
022:    }
023: }
024: 
025: class mainThread implements Runnable
026: {
027:     public void run()
028:     {
029:         JPanel panel = new MousePanel();
030:         JFrame frame = new JFrame("MouseTest");
031:         frame.add(panel);
032:         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
033:         frame.setSize(300,200);
034:         frame.setVisible(true);
035:     }
036: }
037:    
038: class MousePanel extends JPanel
039: {
040:    public MousePanel()
041:    {
042:       squares = new ArrayList<Rectangle2D>();
043:       current = null;
044: 
045:       addMouseListener(new MouseHandler());
046:       addMouseMotionListener(new MouseMotionHandler());
047:    }
048: 
049:    public void paintComponent(Graphics g)
050:    {
051:        super.paintComponent(g);
052:        
053:        Graphics2D g2 = (Graphics2D) g;
054: 
055:       // draw all squares
056:       for (Rectangle2D r : squares)
057:          g2.draw(r);
058:    }
059: 
060:    /**
061:     * Finds the first square containing a point.
062:     * @param p a point
063:     * @return the first square that contains p
064:     */
065:    public Rectangle2D find(Point2D p)
066:    {
067:       for (Rectangle2D r : squares)
068:       {
069:          if (r.contains(p)) return r;
070:       }
071:       return null;
072:    }
073: 
074:    /**
075:     * Adds a square to the collection.
076:     * @param p the center of the square
077:     */
078:    public void add(Point2D p)
079:    {
080:       double x = p.getX();
081:       double y = p.getY();
082: 
083:       current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH,
084:             SIDELENGTH);
085:       squares.add(current);
086:       repaint();
087:    }
088: 
089:    /**
090:     * Removes a square from the collection.
091:     * @param s the square to remove
092:     */
093:    public void remove(Rectangle2D s)
094:    {
095:       if (s == null) return;
096:       if (s == current) current = null;
097:       squares.remove(s);
098:       repaint();
099:    }
100: 
101:    private static final int SIDELENGTH = 10;
102:    private ArrayList<Rectangle2D> squares;
103:    private Rectangle2D current;
104: 
105:    // the square containing the mouse cursor
106: 
107:    private class MouseHandler extends MouseAdapter
108:    {
109:       public void mousePressed(MouseEvent event)
110:       {
111:          // add a new square if the cursor isn't inside a square
112:          current = find(event.getPoint());
113:          if (current == null) add(event.getPoint());
114:       }
115: 
116:       public void mouseClicked(MouseEvent event)
117:       {
118:          // remove the current square if double clicked
119:          current = find(event.getPoint());
120:          if (current != null && event.getClickCount() >= 2) remove(current);
121:       }
122:    }
123: 
124:    private class MouseMotionHandler implements MouseMotionListener
125:    {
126:       public void mouseMoved(MouseEvent event)
127:       {
128:          // set the mouse cursor to cross hairs if it is inside
129:          // a rectangle
130: 
131:          if (find(event.getPoint()) == null) setCursor(Cursor.getDefaultCursor());
132:          else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
133:       }
134: 
135:       public void mouseDragged(MouseEvent event)
136:       {
137:          if (current != null)
138:          {
139:             int x = event.getX();
140:             int y = event.getY();
141: 
142:             // drag the current rectangle to center it at (x, y)
143:             current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
144:             repaint();
145:          }
146:       }
147:    }
148: }


Maintained by John Loomis, updated Wed Mar 12 11:23:18 2008