有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java可以同时拖动多个对象

如何拖动多个选定对象并在框架内移动

当前的实现是,每当用户单击对象时,按SHIFT键,每个新对象都将添加到multiShape

但是,我只能移动单个选定对象

我的代码如下:

public void mouseClicked(MouseEvent clickEvent) {

            clickShape = null;
            int x = clickEvent.getX(); // x-coordinate of point where mouse was
                                        // clicked
            int y = clickEvent.getY(); // y-coordinate of point

            if (clickEvent.isShiftDown()) {
                int top = shapes.size();
                for (int i = 0; i < top; i++) {
                    Shape s = (Shape) shapes.get(i);
                    if (s.containsPoint(x, y)) {
                        s.setColor(Color.RED);
                        multiShape.add(s);
                    }

                }

                repaint();
            } 

            }
        }

我在抽象形状类下定义的移动对象如下:

void moveTo(int x, int y) {
            left += x;
            top += y;
        }

鼠标点击事件:

public void mousePressed(MouseEvent clickEvent) {
            if (dragShape != null) {
                return;
            }

            int x = clickEvent.getX(); // x-coordinate of point
            int y = clickEvent.getY(); // y-coordinate of point
            clickShape = null;
            for (int i = shapes.size() - 1; i >= 0; i--) { // check shapes from
                                                            // front to back
                Shape s = (Shape) shapes.get(i);
                if (s.containsPoint(x, y)) {
                     dragShape = s;
                     prevDragX = x;
                     prevDragY = y;
                    clickShape = s;
                    break;
                }
            }
            // Continue from here
            if (clickShape == null) {
                return;
            } else if (clickEvent.isPopupTrigger()) {
                click.show(this, x - 10, y - 2);
            }  else {

                dragShape = clickShape;
                prevDragX = x;
                prevDragY = y;
            }

        }

        public void mouseDragged(MouseEvent clickEvent) {
            // User has moved the mouse. Move the dragged shape by the same
            // amount.
            if (dragShape == null) {
                return;
            }
            int x = clickEvent.getX();
            int y = clickEvent.getY();
            if (dragShape != null) {
            dragShape.moveTo(x - prevDragX, y - prevDragY);
            prevDragX = x;
            prevDragY = y;
            repaint(); // redraw canvas to show shape in new position
            }
        }

        public void mouseReleased(MouseEvent clickEvent) {
            // User has released the mouse. Move the dragged shape, then set
            // shapeBeingDragged to null to indicate that dragging is over.
            if (dragShape == null) {
                return;
            }
            int x = clickEvent.getX();
            int y = clickEvent.getY();
            if (clickEvent.isPopupTrigger()) {
                click.show(this, x - 10, y - 2);
                // } else {
                dragShape.moveTo(x - prevDragX, y - prevDragY);
                if (dragShape.left >= getSize().width || dragShape.top >= getSize().height
                        || dragShape.left + dragShape.width < 0 || dragShape.top + dragShape.height < 0) { // shape
                    shapes.remove(dragShape); // remove shape from list
                }
                repaint();
            }
            dragShape = null;
        }

共 (1) 个答案

  1. # 1 楼答案

    你只需要调用moveTo来创建一个形状,叫做dragShape,这是你在这里看到的最后一个形状

    for (int i = shapes.size() - 1; i >= 0; i ) { // check shapes from
                                                            // front to back
        Shape s = (Shape) shapes.get(i);
        if (s.containsPoint(x, y)) {
             dragShape = s;
             prevDragX = x;
             prevDragY = y;
             clickShape = s;
             break;
        }
    }
    

    而不是做:

    if (dragShape != null) {
    dragShape.moveTo(x - prevDragX, y - prevDragY);
    

    做:

    for (Shape s : multiShape)
        s.moveTo(x - prevDragX, y - prevDragY);