zoukankan      html  css  js  c++  java
  • java 拖拽矩形

    简介

    java 拖拽矩形

    code

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    import javax.swing.*;
    
    
    public class MouseComponent extends JComponent {
        private static final int DEFAULT_WIDTH = 300;
        private static final int DEFAULT_HEIGHT = 200;
    
        private static final int SIDELENGTH = 10;
        private ArrayList<Rectangle2D> squares;
        private Rectangle2D current;
    
        public MouseComponent() {
            squares = new ArrayList<>();
            current = null;
    
            addMouseListener(new MouseHandler());
            addMouseMotionListener(new MouseMotionHandler());
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        }
    
        public void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
    
            // draw all squares
            for (Rectangle2D r : squares) {
                g2.draw(r);
            }
        }
    
        public Rectangle2D find(Point2D p) {
            for (Rectangle2D r : squares) {
                if (r.contains(p))
                    return r;
            }
            return null;
        }
    
        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 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();
                }
            }
        }
    }
    
    
    import java.awt.*;
    
    import javax.swing.*;
    
    public class MouseFrame extends JFrame{
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                MouseFrame m = new MouseFrame();
                m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                m.setVisible(true);
            });
        }
        public MouseFrame() {
            add(new MouseComponent());
            pack();
        }
    }
    

    image

    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    springboot集成mockito与powermock
    不一样的go语言-玩转语法之二
    不一样的go语言-玩转语法之一
    不一样的go语言-athens源码概览
    不一样的go语言-athens私仓安装
    不一样的go语言-构建系统与构件系统
    不一样的go语言-error
    不一样的go语言-gopher
    jssip中文开发文档(完整版)
    echarts属性的设置(完整大全)
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13906100.html
Copyright © 2011-2022 走看看