Mouse Events
A Java application receives a steady stream of mouse events. The cause of a mouse event is indicated by the values of its member variables:
MOUSE_CLICKED
MOUSE_DRAGGED
MOUSE_ENTERED
MOUSE_EXITED
MOUSE_FIRST
MOUSE_LAST
MOUSE_MOVED
MOUSE_PRESSED
MOUSE_RELEASED
A mouse event also encapsulates the position of the mouse, which can be learned by calling its getPoint() method, and the number of times the mouse is clicked, which can be learned by calling the getClickCount() method. (Left and right button clicks are not distinguished because some mice don't have two buttons.)
There are two types of mouse event listeners. A MouseMotionListener handles MOUSE_MOVED and MOUSE_DRAGGED events. The other, less common, types of mouse events are handled by MouseListener objects.
Example: Draw
Draw is a simple mouse drawing program. Dragging the mouse, i.e., moving the mouse while the mouse button is down, changes the cursor, adds points to a point vector, and repaints a small rectangle around the new point. A double click empties the point vector and repaints the entire window.
The paint() method traverses the vector of points. For each point p, a red circle is drawn in a small rectangle with upper left corner p.
Here are some import statements you will need:
import java.awt.*;
import java.awt.event.*;
import java.util.*; // for Vector
import pearce.java.util.*; // for MainFrame
class Draw extends MainFrame
{
private Vector drawing; // points in drawing
private int brushWidth; // diameter of circle
private int red, green, blue; // color of circle
private boolean DEBUG; // println mouse position?
public Draw()
{
setTitle("Draw");
addMouseMotionListener(new MouseMotionHandler());
addMouseListener(new MouseEventHandler());
drawing = new Vector();
brushWidth = 5;
red = 255;
green = blue = 0;
DEBUG = false;
}
class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent e)
{
if (DEBUG)
{
setCursor(Cursor.getPredefinedCursor(MOVE_CURSOR));
Point p = e.getPoint();
System.out.println("(" + p.x + ", " + p.y + ")");
}
}
public void mouseDragged(MouseEvent e)
{
setCursor(Cursor.getPredefinedCursor(CROSSHAIR_CURSOR));
Point p = e.getPoint();
drawing.addElement(p);
// to prevent flicker, only repaint a small rectangle
repaint(p.x, p.y, brushWidth, brushWidth);
}
}
class MouseEventHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() > 1)
{
drawing.removeAllElements();
repaint(); // repaint entire window
}
}
public void mouseReleased(MouseEvent e)
{
setCursor(Cursor.getDefaultCursor());
}
}
public void paint(Graphics g)
{
g.setColor(new Color(red, green, blue));
for(int i = 0; i < drawing.size(); i++)
{
Point p = (Point)drawing.elementAt(i);
g.fillOval(p.x, p.y, brushWidth, brushWidth);
}
}
// setters (not used here)
public void setBrushWidth(int x) { brushWidth = x; }
public void setBrushColor(int r, int g, int b)
{
red = r;
green = g;
blue = b;
}
public void setDebug(boolean flag) { DEBUG = flag; }
// test harness
public static void main(String[] args)
{
Draw f = new Draw();
f.show();
}
} // Draw
Program Output
AWT Geometric Types
Point is a predefined geometric type in the AWT package:
class Point
{
public int x, y;
// etc.
}
Here are some others:
class Rectangle
{
public int x, y, height, width;
// etc.
}
class Dimension
{
public int width, height;
// etc.
}
class Polygon
{
public int[] xpoints, ypoints;
// etc.
}
Problem
Modify the Draw application so users can make multi color drawings using multiple brush widths. Instead of a vector of points, drawing will need to be a vector of brush strokes, where:
class BrushStroke
{
public Point point;
public int red, green, blue;
public int penWidth;
// etc.
}
A modeless dialog box should be used to change the brush width and color. The dialog is displayed in response to a menu selection.
How can you save a drawing to a file and reload it later?