public class DrawRectangleMouseHandler extends MouseAdapter {
private PaintPanel paintPanel;
private Point2D start;
private Point2D end;
public DrawRectangleMouseHandler(PaintPanel paintPanel) {
this.paintPanel = paintPanel;
}
public void mousePressed(MouseEvent event) {
start = event.getPoint();
paintPanel.setStartPoint(start);
}
public void mouseReleased(MouseEvent event) {
end = event.getPoint();
Rectangle2D rect = ShapeUtil.getRectangle2D(start, end);
paintPanel.addShape(rect);
public class DrawRectangleMouseMotionHandler extends MouseMotionAdapter {
private PaintPanel paintPanel;
public DrawRectangleMouseMotionHandler(PaintPanel paintPanel) {
this.paintPanel = paintPanel;
}
public void mouseDragged(MouseEvent event) {
Point2D start = paintPanel.getStartPoint();
Point2D end = event.getPoint();
Rectangle2D rect = ShapeUtil.getRectangle2D(start, end);
paintPanel.setDrawingShape(rect);
}
}
/*
ExitAction.java
*/
package com.jkzqw.painter;
import java.awt.event.*;
import javax.swing.*;
public class ExitAction extends AbstractAction {
private static final long serialVersionUID = 1L;
private JFrame parent;
public ExitAction(JFrame parent) {
this.parent = parent;
this.putValue(Action.NAME, "Exit");
this.putValue(Action.SHORT_DESCRIPTION, "Exit the Painter");
this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl X"));
}
public void actionPerformed(ActionEvent e) {
doExit();
}
public void doExit() {
int result = JOptionPane.showConfirmDialog(parent,
"Exit the Painting application?", "Exit",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION)
System.exit(0);
}
public class DrawLineMouseHandler extends MouseAdapter {
private Point2D start;
private Point2D end;
private PaintPanel paintPanel;
public DrawLineMouseHandler(PaintPanel paintPanel) {
this.paintPanel = paintPanel;
}
public void mousePressed(MouseEvent event) {
start = event.getPoint();
paintPanel.setStartPoint(start);
}
public void mouseReleased(MouseEvent event) {
end = event.getPoint();
Line2D line = new Line2D.Double(start, end);
paintPanel.addShape(line);
paintPanel.removeDrawingShape();
}
}
public class MouseTest
{
public static void main(String[] args)
{
MouseFrame frame = new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
A frame containing a panel for testing mouse operations
*/
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("MouseTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
MousePanel panel = new MousePanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
/**
A panel with mouse operations for adding and removing squares.
*/
class MousePanel extends JPanel
{
public MousePanel()
{
squares = new ArrayList<Rectangle2D>();
current = null;
// draw all squares
for (Rectangle2D r : squares)
g2.draw(r);
}
/**
Finds the first square containing a point.
@param p a point
@return the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r : squares)
{
if (r.contains(p)) return r;
}
return null;
}
/**
Adds a square to the collection.
@param p the center of the square
*/
public void add(Point2D p)
{
double x = p.getX();
double y = p.getY();
current = new Rectangle2D.Double(
x - SIDELENGTH / 2,
y - SIDELENGTH / 2,
SIDELENGTH,
SIDELENGTH);
squares.add(current);
repaint();
}
/**
Removes a square from the collection.
@param s the square to remove
*/
public void remove(Rectangle2D s)
{
if (s == null) return;
if (s == current) current = null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
// the square containing the mouse cursor
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
// add a new square if the cursor isn't inside a square
current = find(event.getPoint());
if (current == null)
add(event.getPoint());
}
public void mouseClicked(MouseEvent event)
{
// remove the current square if double clicked
current = find(event.getPoint());
if (current != null && event.getClickCount() >= 2)
remove(current);
}
}
private class MouseMotionHandler
implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
// set the mouse cursor to cross hairs if it is inside
// a rectangle
if (find(event.getPoint()) == null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event)
{
if (current != null)
{
int x = event.getX();
int y = event.getY();
// drag the current rectangle to center it at (x, y)
current.setFrame(
x - SIDELENGTH / 2,
y - SIDELENGTH / 2,
SIDELENGTH,
SIDELENGTH);
repaint();
}
}
}
}