JAVA鼠标屏幕绘制拖拽删除矩形
来源:互联网 发布:php软件开发工程师 编辑:程序博客网 时间:2024/05/24 03:26
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.ArrayList;
import javax.swing.JComponent;
public class MouseComponent extends JComponent {
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
public MouseComponent() {
this.squares = new ArrayList<Rectangle2D>();
this.current = null;
this.addMouseListener(new MouseHandle());//监听鼠标点击事件
this.addMouseMotionListener(new MouseMotionHandler());//监听鼠标移动事件
}
//如果鼠标单击屏幕的地方有矩形则不绘制矩形
public Rectangle2D find(Point2D p) {
for (Rectangle2D r : squares) {
if (r.contains(p))
return r;
}
return null;
}
//绘制矩形
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Rectangle2D r : squares) {
g2.draw(r);
}
}
//添加矩形到屏幕
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();
}
//删除矩形
public void remove(Rectangle2D s) {
if (s == null)
return;
if (s == current)
current = null;
squares.remove(s);
repaint();
}
private class MouseHandle extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
current = find(e.getPoint());
//当前位置如果有矩形,且点击大于2次,则删除矩形
if (current != null && e.getClickCount() >= 2)
remove(current);
}
@Override
public void mousePressed(MouseEvent e) {
current = find(e.getPoint());
//当前位置没有矩形则绘制
if (current == null)
add(e.getPoint());
}
}
private class MouseMotionHandler implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent e) {
//如果当前位置有矩形,进行拖拽。
if (current != null) {
int x = e.getX();
int y = e.getY();
current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
SIDELENGTH, SIDELENGTH);
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
//鼠标在空白屏幕默认箭头
if(find(e.getPoint())==null)setCursor(Cursor.getDefaultCursor());
//碰到矩形区域区域变成十字架
else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
}
}
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.util.ArrayList;
import javax.swing.JComponent;
public class MouseComponent extends JComponent {
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
public MouseComponent() {
this.squares = new ArrayList<Rectangle2D>();
this.current = null;
this.addMouseListener(new MouseHandle());//监听鼠标点击事件
this.addMouseMotionListener(new MouseMotionHandler());//监听鼠标移动事件
}
//如果鼠标单击屏幕的地方有矩形则不绘制矩形
public Rectangle2D find(Point2D p) {
for (Rectangle2D r : squares) {
if (r.contains(p))
return r;
}
return null;
}
//绘制矩形
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Rectangle2D r : squares) {
g2.draw(r);
}
}
//添加矩形到屏幕
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();
}
//删除矩形
public void remove(Rectangle2D s) {
if (s == null)
return;
if (s == current)
current = null;
squares.remove(s);
repaint();
}
private class MouseHandle extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
current = find(e.getPoint());
//当前位置如果有矩形,且点击大于2次,则删除矩形
if (current != null && e.getClickCount() >= 2)
remove(current);
}
@Override
public void mousePressed(MouseEvent e) {
current = find(e.getPoint());
//当前位置没有矩形则绘制
if (current == null)
add(e.getPoint());
}
}
private class MouseMotionHandler implements MouseMotionListener {
@Override
public void mouseDragged(MouseEvent e) {
//如果当前位置有矩形,进行拖拽。
if (current != null) {
int x = e.getX();
int y = e.getY();
current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
SIDELENGTH, SIDELENGTH);
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {
//鼠标在空白屏幕默认箭头
if(find(e.getPoint())==null)setCursor(Cursor.getDefaultCursor());
//碰到矩形区域区域变成十字架
else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
}
}
0 0
- JAVA鼠标屏幕绘制拖拽删除矩形
- 使用Java Swing绘制随鼠标拖拽可见轨迹的矩形(不会一直绘制矩形,而是类似ps中的矩形工具)
- SilverLight鼠标绘制矩形
- 使用鼠标绘制矩形
- 鼠标绘制矩形
- 鼠标绘制矩形
- MFC 鼠标绘制矩形框
- winfrom-拖动鼠标绘制矩形
- OpenCV鼠标画图例程,鼠标绘制矩形
- OpenCV鼠标画图例程,鼠标绘制矩形
- OpenCV鼠标画图例程,鼠标绘制矩形
- Opencv中利用鼠标绘制矩形
- 鼠标在窗口中绘制矩形
- 用鼠标在窗口中绘制矩形
- 如何重写PictureBox的OnPaint方法绘制矩形,并实现拖动,鼠标悬停时提示拖拽方向
- 如何重写PictureBox的OnPaint方法绘制矩形,并实现拖动改变矩形大小,鼠标悬停时提示拖拽方向(二)
- OpenCV鼠标绘制矩形和截取图像的矩形区域
- opencv鼠标绘制矩形和截取图像的矩形区域
- 最小费用流
- linux下最好的恢复文章
- APUE第7章 UNIX进程环境 笔记
- MES核心模块功能设计
- OEM创建出错
- JAVA鼠标屏幕绘制拖拽删除矩形
- HTML5边玩边学(1):画布
- 关于百度蜘蛛IP详解————————【Badboy】
- android 隐藏状态栏,全屏,背景模糊,不能横屏
- Maven 手动安装Jar包的例子
- Minimum Path Sum
- [shell]Shell简介
- 华为公司软件编程规范
- JAVA 对象数组,加载图片实例