Java简单图形编辑器 放置、移动和擦除方块 鼠标事件测试程序 Java核心技术 事件处理 source code: package com.sunnyykn.chapter08; import java.awt.*; import java.awt.event.*; import java.util.*; import java.awt.geom.*; import javax.swing.*; /** * * A panel with mouse operations for adding and removing squares. * @author sunnyykn * */ class MousePanel extends JPanel { private static final int SIDELENGTH = 10; private ArrayList<Rectangle2D> squares; private Rectangle2D current; //the square containing the mouse cursor public MousePanel() { squares = new ArrayList<Rectangle2D>(); current = null; addMouseListener(new MouseHandler()); addMouseMotionListener(new MouseMotionHandler()); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; //draw all squares for (Rectangle2D r : squares) g2.draw(r);