我正在绘制许多可以移动的线,可以调整大小。但是,一旦单击先前创建的线,它就不会移动。只有最新创建的线在移动。任何建议都可以使我的所有线随时移动。

MouseAdapter mouseAdapter = new MouseAdapter() {

        public void mousePressed(MouseEvent e) {
            Rectangle rec = new Rectangle(e.getX(), e.getY(), 50, 50);
            prevPoint = e.getPoint();
            updateAction(e);
            ignoreMoves = true;

            System.out.println("Size:"+shapes.size());

            for (Shape shape : shapes) {
                if(shape.contains(prevPoint)){
                    System.out.println("Clicked Position is within a shape");
                    currentShape=shape;
                }
                else{
                    System.out.println("Clicked Position NOT within a shape");
                }
            }
            repaint();
        }

        public void mouseDragged(MouseEvent e) {
            System.out.println("mouseDragged action:" + action);
            if (action != null) {
                switch (action) {
                case Move: {
                    //System.out.println("Move");
                    int dx = 0;
                    int dy = 0;

                    dx = (int) (prevPoint.x - e.getPoint().getX());
                    dy = (int) (prevPoint.y - e.getPoint().getY());

                    Line2D shape = ( Line2D ) currentShape;

                    int x1 = (int) (shape.getX1() - dx);
                    int y1 = (int) (shape.getY1() - dy);

                    int x2 = (int) (shape.getX2() - dx);
                    int y2 = (int) (shape.getY2() - dy);

                    Point startPoint = new Point(x1, y1);
                    Point endPoint = new Point(x2, y2);

                    if (shape != null) {
                        shape.setLine(startPoint, endPoint);
                        prevPoint = e.getPoint();
                        repaint();
                    }
                }
                    break;
                case ResizeEast: {
                      Line2D shape = ( Line2D ) currentShape;
                    Point endPointLine = e.getPoint();
                    shape.setLine(shape.getP1(), endPointLine);
                    repaint();
                }
                    break;
                }
            }
        }

        public void mouseMoved(MouseEvent e) {
            if (!ignoreMoves) {
                updateAction(e);
            }
        };

        protected void updateAction(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();

            Point clickedPoint = new Point(x, y);

            int width = 0;
            int height = 0;

            double startPointX = 0;
            double startPointY = 0;
            double endPointX = 0;
            double endPointY = 0;

            if (shapes != null && shapes.size() > 0) {
                  Line2D shape = ( Line2D ) currentShape;

                Line2D line = (Line2D) shape;
                startPointX = line.getX1();
                startPointY = line.getY1();
                endPointX = line.getX2();
                endPointY = line.getY2();

                Rectangle rect = shape.getBounds();
                width = rect.width;
                height = rect.height;


                if (x > endPointX - 5) {
                    action = MouseAction.ResizeEast;
                } else if (rect.contains(clickedPoint)) {
                    action = MouseAction.Move;
                } else {
                    action = null;
                }
            }

            if (action != null) {
                setCursor(action.getCursor());
            } else {
                setCursor(null);
            }
        }

    };
    protected void paintComponent(Graphics g) {
        //System.out.println("Paint");

        Graphics2D g2d = (Graphics2D) g;
        g2d.setPaint(Color.BLACK);

        if (shapes != null && shapes.size() > 0) {

            for (Shape shape : shapes) {
                g2d.draw(shape);
            }
        } else {
            System.out.println("Shapes is Null");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Point startPoint = new Point(50, 200);
        Point endPoint = new Point(100, 200);
        currentShape = new Line2D.Double(startPoint, endPoint);
        shapes.add(currentShape);
        repaint();
    }

最佳答案

您的问题出在Line2D上。contains

如果您阅读该文档,则会显示:


返回:false,因为Line2D不包含区域。


我建议的解决方案是创建矩形2D而不是Line2D,其中宽度或高度实际上是Stroke的大小(请参见Stroke)。另外,您还必须旋转Rectangle2D。最简单的方法可能是将它们转换为Area(请参见Area),这是我创建similar type of shape container时所使用的

09-26 01:05