Java 使用鼠标编辑形状位置

Java 使用鼠标编辑形状位置,java,swing,awt,java-2d,Java,Swing,Awt,Java 2d,我想移动我以前创建的形状对象。我不知道如何更改(更新)形状位置。现在我得到了一个cast错误: java.awt.geom.Ellipse2D$Double cannot be cast to java.awt.Graphics2D 现在我可以访问特定的形状,但它似乎没有setLocation()或类似的东西 请给我小费,因为我卡住了 Frame 抽屉 公共类抽屉扩展JPanel实现MouseListener、MouseMotionListener{ 私有最终int defaultSize=5

我想移动我以前创建的
形状
对象。我不知道如何更改(更新)形状位置。现在我得到了一个cast错误:

java.awt.geom.Ellipse2D$Double cannot be cast to java.awt.Graphics2D
现在我可以访问特定的形状,但它似乎没有
setLocation()
或类似的东西

请给我小费,因为我卡住了

Frame
抽屉
公共类抽屉扩展JPanel实现MouseListener、MouseMotionListener{
私有最终int defaultSize=50;
私有颜色defaultColor=Color.BLACK;
私有整数oldX,oldY,currentX,currentY;
公共字符串currentShape=“”;
公共布尔编辑模式=false;
私有int-activeShape;
私有最终ArrayList形状=新ArrayList();
私有最终ArrayList颜色=新ArrayList();
公共抽屉(组件…组件){
用于(组件c:组件){
c、 addMouseListener(这个);
c、 addMouseMotionListener(此);
}
颜色。添加(颜色。黑色);
}
@凌驾
公共无效鼠标按下(MouseEvent e){
Point=this.getMousePosition();
电流x=点x;
电流y=点y;
如果(编辑模式){
对于(int i=0;i
您没有提供(您的代码无法由我编译),因此我无法测试我的解决方案,但您在方法
中使用的方法是错误的。要平移需要替换的形状,请执行以下操作:

Graphics2D g = (Graphics2D) shape;
g.translate(currentX,currentY);


如果没有帮助,请提供解决方案。

谢谢,此解决方案有效。我添加了transform.translate(currentX-previousX,currentY-previousY);以获得移动的正确偏移。注意。下次我会送SSCCE
public class Drawer extends JPanel implements MouseListener, MouseMotionListener {

        private final int defaultSize = 50;
        private Color defaultColor = Color.BLACK;
        private int oldX, oldY, currentX, currentY;
        public String currentShape = "";
        public Boolean editMode = false;
        private int activeShape;
        private final ArrayList<Shape> shapes = new ArrayList<>();
        private final ArrayList<Color> colors = new ArrayList<>();

        public Drawer(Component... components) {
            for (Component c : components) {
                c.addMouseListener(this);
                c.addMouseMotionListener(this);
            }
            colors.add(Color.BLACK);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            Point point = this.getMousePosition();
            currentX = point.x;
            currentY = point.y;
            if (editMode) {
                for (int i = 0; i < shapes.size(); i++) {
                    Shape shape = shapes.get(i);
                    if (shape.contains(e.getPoint())) {
                        System.out.println("Clicked shape " + i);
                        activeShape = i;
                    }
                }
            } else {
                switch (currentShape) {
                    case "circle":
                        Ellipse2D ellipse2D = new Ellipse2D.Double(currentX, currentY, defaultSize, defaultSize);
                        shapes.add(ellipse2D);
                        colors.add(defaultColor);
                        break;
                    case "rec":
                        Rectangle2D rectangle2D = new Rectangle2D.Double(currentX, currentY, defaultSize, defaultSize);
                        shapes.add(rectangle2D);
                        colors.add(defaultColor);
                        break;
                }
                repaint();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            Point point = this.getMousePosition();
            currentX = point.x;
            currentY = point.y;        
            if(editMode){
                Shape shape = shapes.get(activeShape);
                Graphics2D g = (Graphics2D) shape;
                g.translate(currentX,currentY);
            }
            repaint();        
        }   

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;    
            for (int i = 0; i < shapes.size(); i++) {
                Shape shape = shapes.get(i);
                Color color = colors.get(i);
                g2.setColor(color);
                g2.fill(shape);
            }
        }
    }
Graphics2D g = (Graphics2D) shape;
g.translate(currentX,currentY);
AffineTransform transform = new AffineTransform();
transform.translate(currentX, currentY);
shape = transform.createTransformedShape(shape);
shapes.set(activeShape, shape);