Java 移动形状的原始形状不会被删除,也不会添加到形状中

Java 移动形状的原始形状不会被删除,也不会添加到形状中,java,graphics,mouseevent,shapes,affinetransform,Java,Graphics,Mouseevent,Shapes,Affinetransform,大约6小时前,我发布了一个问题,无法开始创建新功能 但我取得了进步。但另一个障碍出现了 我做了拖动形状的功能 但有两个问题 I拖动和移动的原始形状形状形状仍然存在(未删除自身) 应将原始形状从形状中删除,并将新移动的形状添加到形状中 我上了两门课 这是图形面板类 package frame; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; i

大约6小时前,我发布了一个问题,无法开始创建新功能

但我取得了进步。但另一个障碍出现了

我做了拖动形状的功能

但有两个问题

  • I拖动和移动的原始形状形状形状仍然存在(未删除自身)
  • 应将原始形状从形状中删除,并将新移动的形状添加到形状中
  • 我上了两门课


    这是图形面板类

    package frame;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Shape;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseWheelEvent;
    import java.awt.event.MouseWheelListener;
    import java.awt.geom.AffineTransform;
    import java.util.Collection;
    import java.util.Vector;
    
    import javax.swing.JPanel;
    
    import main.GConstants.EAnchorLocation;
    import main.GConstants.EDrawingStyle;
    import shapeTool.GShapeTool;
    
    public class GPanel extends JPanel {
    
    private static final long serialVersionUID = 1L;
    
    private GShapeTool shapeTool;
    private GShapeTool selectedShape;
    private Vector<GShapeTool> shapes;
    
    public GPanel() {
        this.setBackground(Color.WHITE);
    
        this.shapes = new Vector<GShapeTool>();
    
        MouseHandler mouseHandler = new MouseHandler();
        this.addMouseListener(mouseHandler);
        this.addMouseMotionListener(mouseHandler);
        this.addMouseWheelListener(mouseHandler);
    
    }
    
    public void initialize() {
    
    }
    
    public Vector<GShapeTool> getShapes() {
        return this.shapes;
    }
    
    public void setShapes(Vector<GShapeTool> shapes){
        this.shapes = shapes;
        this.updateUI();
    }
    
    
    public void paint(Graphics graphics) {
        super.paint(graphics);
        for(GShapeTool shape: this.shapes) {
            shape.draw((Graphics2D)graphics);
        }
    }
    
    public void setShapeTool(GShapeTool shapeTool) {
        this.shapeTool = shapeTool;
    }
    
    private boolean onShape(int x, int y) {
        boolean shapeCheck = false;
        for(GShapeTool shapeTool: this.shapes) {
            if(shapeTool.contains(x, y)) {
                this.selectedShape = shapeTool;
                shapeCheck = true;
            } else {
                shapeTool.setSelected(false, (Graphics2D)getGraphics());
            }
        }
        if(shapeCheck) {
            this.selectedShape.setSelected(true, (Graphics2D)getGraphics());
        }
        return shapeCheck;
    }       
    
    private void setStartPoint(Point start) {
        this.selectedShape.setStart(start);
        for(GShapeTool shapeTool: this.shapes) {
            if(shapeTool.contains(start.x, start.y)) {
                this.selectedShape = shapeTool;
                
            }
        }
    }
    
    private void setEndPoint(Point end) {
        this.selectedShape.setEnd(end);
        this.selectedShape.move((Graphics2D)getGraphics());
        this.selectedShape.clearPoint();
    }
    
    private void setInitialPoint(int x, int y) {
        this.selectedShape = this.shapeTool.clone();
        this.selectedShape.setInitialPoint(x, y);
    }
    
    
    
    private void setFinalPoint(int x, int y) {
        for(GShapeTool shapeTool: this.shapes) {
            shapeTool.setSelected(false, (Graphics2D)getGraphics());
        }
        this.selectedShape.setFinalPoint(x, y);
        this.selectedShape.setSelected(true, (Graphics2D)getGraphics());
        this.shapes.add(this.selectedShape);
    }
    
    private void setIntermediatePoint(int x, int y) {
        this.selectedShape.setIntermediatePoint(x, y);  
    }
    
    private void animate(int x, int y) {
        Graphics2D graphics2D = (Graphics2D) this.getGraphics();
        //set xor mode;
        graphics2D.setXORMode(getBackground());
        this.selectedShape.animate(graphics2D, x, y);
    }
    
    
    
    
    private class MouseHandler 
    implements MouseListener, MouseMotionListener, MouseWheelListener {
    
        private boolean isDrawing;
        private boolean isMoving;
        MouseHandler() {
            this.isDrawing = false;
            this.isMoving = false;
        }
    
        @Override
        public void mouseClicked(MouseEvent e) {
    
            if(e.getButton() == MouseEvent.BUTTON1) {
                if(e.getClickCount() == 1) {
                    this.mouseLButton1Clicked(e);
                } else if(e.getClickCount() == 2) {
                    this.mouseLButton2Clicked(e);
                }   
            } else if(e.getButton() == MouseEvent.BUTTON2) {
                if(e.getClickCount() == 1) {
                    this.mouseRButton1Clicked(e);
                }
            }
        }
    
    
    
        @Override
        public void mouseMoved(MouseEvent e) {  
            if(isDrawing) {
                if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
                    animate(e.getX(), e.getY());
                }
            }
        }
    
        private void mouseLButton1Clicked(MouseEvent e) {
            if(!this.isDrawing) {
                if(!onShape(e.getX(), e.getY())) {
                    if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
                        setInitialPoint(e.getX(), e.getY());
                        this.isDrawing = true;
                    }
                }
                else if(onShape(e.getX(), e.getY())) {
                    onShape(e.getX(), e.getY());
                }
    
            } else {
                if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
                    setIntermediatePoint(e.getX(),e.getY());
                }
            }   
        }
    
        private void mouseLButton2Clicked(MouseEvent e) {
            if(this.isDrawing) {
                if(shapeTool.getDrawingStyle() == EDrawingStyle.eNPointDrawing) {
                    setFinalPoint(e.getX(), e.getY());
                    this.isDrawing = false;
                }
            }
        }
    
        private void mouseRButton1Clicked(MouseEvent e) {
    
        }
    
        @Override
        public void mousePressed(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON1) {
                if(onShape(e.getX(), e.getY())) {
                    if(!isMoving) {
                        setStartPoint(e.getPoint());    
                        this.isMoving = true;
                    }                   
                } else if(!onShape(e.getX(), e.getY())) {
                    if(!this.isDrawing) {
                        if(shapeTool.getDrawingStyle() == EDrawingStyle.e2PointDrawing) {
                            setInitialPoint(e.getX(), e.getY());
                            this.isDrawing = true;
                        } 
                    }
                }               
            }
        }
        @Override
        public void mouseDragged(MouseEvent e) {
            if(this.isDrawing) {
                if(shapeTool.getDrawingStyle() == EDrawingStyle.e2PointDrawing) {
                    animate(e.getX(), e.getY());
                }
            } 
        }
    
        @Override
        public void mouseReleased(MouseEvent e) {
            if(isMoving) {
                setEndPoint(e.getPoint());
                this.isMoving = false;
            } else if(this.isDrawing) {
                if(shapeTool.getDrawingStyle() == EDrawingStyle.e2PointDrawing) {
                    setFinalPoint(e.getX(), e.getY());
                    this.isDrawing = false;
                }
            }
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {}
        @Override
        public void mouseExited(MouseEvent e) {}        
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {}
    
        }
    }
    

    package shapeTool;
    
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.Rectangle;
    import java.awt.Shape;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Ellipse2D;
    import java.io.Serializable;
    
    import main.GConstants.EDrawingStyle;
    
    public abstract class GShapeTool implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    public enum EAnchors {
        x0y0,
        x0y1,
        x0y2,
        x1y0,
        x1y2,
        x2y0,
        x2y1,
        x2y2,
        RR;
    }
    
    public final static int wAnchor = 10;
    public final static int hAnchor = 10;
    
    private EDrawingStyle eDrawingStyle;
    
    protected boolean isSelected;
    
    protected Point start, end;
    
    protected Shape shape;
    //  private Shape[] anchors;
    private Ellipse2D[] anchors;
    
    private AffineTransform affineTransform;
    
    public GShapeTool(EDrawingStyle eDrawingStyle) {
        this.eDrawingStyle = eDrawingStyle;
        this.isSelected = false;
        this.anchors = new Ellipse2D.Double[EAnchors.values().length];
        for(EAnchors eAnchor: EAnchors.values()) {
            this.anchors[eAnchor.ordinal()] = new Ellipse2D.Double();
        }
        this.affineTransform = new AffineTransform();
    }
    
    public EDrawingStyle getDrawingStyle() {
        return this.eDrawingStyle;
    }
    
    public boolean contains(int x, int y) {
        return this.shape.contains(x , y);
    }
    
    public void setStart(Point start) {
        this.start = start;
    }
    
    public Point getStart() {
        return this.start;
    }
    
    public void setEnd(Point end) {
        this.end = end;
    }
    
    public void clearPoint() {
        this.start = null;
        this.end = null;
    }
    
    public Shape getShape() {
        return this.shape;
    }
    
    public Shape move(Graphics2D graphics2D) {
        affineTransform.translate(this.end.getX() - this.start.getX(), this.end.getY() - this.start.getY());
        Shape moved = affineTransform.createTransformedShape(this.shape);
        affineTransform = new AffineTransform();
        graphics2D.draw(moved);
        return moved;
    }
    
    private void drawAnchors(Graphics2D graphics2D) {
        // draw bounding rectangle
        Rectangle rectangle = this.shape.getBounds();
        int x0 = rectangle.x-wAnchor;
        int x1 = rectangle.x + rectangle.width/2;
        int x2 = rectangle.x + rectangle.width;
        int y0 = rectangle.y-hAnchor;
        int y1 = rectangle.y + rectangle.height/2;
        int y2 = rectangle.y + rectangle.height;
    
        this.anchors[EAnchors.x0y0.ordinal()].setFrame(x0,y0,wAnchor,hAnchor);
        this.anchors[EAnchors.x0y1.ordinal()].setFrame(x0,y1,wAnchor,hAnchor);
        this.anchors[EAnchors.x0y2.ordinal()].setFrame(x0,y2,wAnchor,hAnchor);
        this.anchors[EAnchors.x1y0.ordinal()].setFrame(x1,y0,wAnchor,hAnchor);
        this.anchors[EAnchors.x1y2.ordinal()].setFrame(x1,y2,wAnchor,hAnchor);
        this.anchors[EAnchors.x2y0.ordinal()].setFrame(x2,y0,wAnchor,hAnchor);
        this.anchors[EAnchors.x2y1.ordinal()].setFrame(x2,y1,wAnchor,hAnchor);
        this.anchors[EAnchors.x2y2.ordinal()].setFrame(x2,y2,wAnchor,hAnchor);
        this.anchors[EAnchors.RR.ordinal()].setFrame(x1,y0-50,wAnchor,hAnchor);
        //draw anchors
        graphics2D.setColor(Color.BLACK);
        for(EAnchors eAnchor: EAnchors.values()) {
            graphics2D.draw(this.anchors[eAnchor.ordinal()]);
        }
    }
    
    private void eraseAnchors(Graphics2D graphics2D) {
        graphics2D.setColor(Color.WHITE);
        for(EAnchors eAnchor: EAnchors.values()) {
            graphics2D.draw(this.anchors[eAnchor.ordinal()]);
        }       
    }
    
    public void setSelected(boolean isSelected, Graphics2D graphics2D) {
        if(this.isSelected) {
            if(!isSelected) {
                //erase
                this.eraseAnchors(graphics2D);
            }
    
        } else {
            if(isSelected) {
                //draw
                this.drawAnchors(graphics2D);
            }
    
        }
        this.isSelected = isSelected;
    }
    
    public void draw(Graphics2D graphics) {
        graphics.draw(this.shape);  
    }
    
    public void animate(Graphics2D graphics2d, int x, int y) {
        //erase;
        this.draw(graphics2d);
        //      //move point
        this.movePoint(x,y);
        //      //draw;
        this.draw(graphics2d);
    }
    
    
    //interface
    public abstract GShapeTool clone();
    
    public abstract void setInitialPoint(int x, int y);
    
    public abstract void setFinalPoint(int x, int y);
    
    public abstract void setIntermediatePoint(int x, int y);
    
    public abstract void movePoint(int x, int y);
    
    
    
    }