Java简单图形编辑器 放置、移动和擦除方块 鼠标事件测试程序 Java核心技术 事件处理
2010-12-30 20:53
1046 查看
Java简单图形编辑器 放置、移动和擦除方块 鼠标事件测试程序 Java核心技术 事件处理
source code:
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); } /** * Finds the first square containing a point * @param p a point * @return the first square that contains p * @author sunnyykn * */ public Rectangle2D find(Point2D p) { for (Rectangle2D r : squares) { if (r.contains(p)) return r; } return null; } /** * Add a square to the collection * @param p the centre of the square * @author sunnyykn * */ 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 * @author sunnyykn * */ public void remove(Rectangle2D s) { if (s == null) return; if (s == current) current = null; squares.remove(s); repaint(); } 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(); } } } } /** * A frame containing a panel for testing mouse operations * @author sunnyykn * */ class MouseFrame extends JFrame { public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; public MouseFrame() { setTitle("MouseTest Sunnyykn"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); //add panel to frame MousePanel panel = new MousePanel(); add(panel); } } public class MouseTest { public static void main(String args[]) { MouseFrame frame = new MouseFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
相关文章推荐
- java例程练习(简单的画图程序[鼠标事件处理])
- java例程练习(简单的画图程序[鼠标事件处理])
- 2015 7 3 java核心技术卷一 7-10章 图形,事件处理,Swing组件和应用程序部署
- Java 按钮和单击事件映射到动作对象改变面板颜色小程序实例 Java核心技术 事件处理
- Java处理敲击键盘事件 Etch-A-Sketch玩具实现 光标画笔画图程序 Java核心技术
- 小博老师解析Java核心技术 ——JSwing鼠标监听事件
- [Java GUI] 鼠标移动事件处理示例
- 小博老师解析Java核心技术 ——JSwing鼠标手势监听事件
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- 小博老师解析Java核心技术 ——JSwing鼠标监听事件
- java核心技术笔记 事件处理
- Java核心技术(事件处理)
- Java核心技术:卷1笔记[6] 事件处理
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- 事件处理基础 处理按钮点击事件实例 Java核心技术
- #Java 核心技术卷一阅读笔记# 第十一章 事件处理
- 简单的计算器程序(摘自java核心技术.卷I)
- 小博老师解析Java核心技术 ——JSwing鼠标手势监听事件
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- java核心技术 事件处理