• JAVA鼠标屏幕绘制拖拽删除矩形


    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));
    }


    }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Kubernetes源码client-go的workqueue
    (转)Golang语言heap剖析及利用heap实现优先级队列
    (转)Kubernetes GC设计原则
    Kubernetes kubefed使用Cluster Selector
    使用kubeadm添加新节点到集群及相关问题解决
    Kubeadm颁发证书延迟到10年
    kubeadm升级Kubernetes证书
    混合kubebuilder与code generator编写CRD
    (转)Go项目的vendor目录是否需要提交?看这一篇就知道了
    Java 将Excel转为et和ett格式
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656594.html
Copyright © 2020-2023  润新知