使用鼠标编辑形状位置

时间:2018-04-24 10:13:14

标签: java swing awt java-2d

我想移动我之前创建的Shape个对象。我无法弄清楚如何更改(更新)形状位置。现在我收到施法错误:

java.awt.geom.Ellipse2D$Double cannot be cast to java.awt.Graphics2D

现在我可以访问特定的形状,但它似乎没有setLocation()或类似的东西。

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

Frame

public class Frame {

        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(600, 600);
            frame.setTitle("Prosty Paint");
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            Drawer drawer = new Drawer(frame.getComponents());
            Menu menu = new Menu(drawer);
            frame.setJMenuBar(menu.menuBar);
            frame.add(drawer);
            frame.setVisible(true);
        }
}

Drawer

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);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您尚未提供SSCCE(您的代码无法由我编译)因此我无法测试我的解决方案,但您在方法mouseReleased中的方法是错误的。要翻译您需要替换的形状

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

通过

AffineTransform transform = new AffineTransform();
transform.translate(currentX, currentY);
shape = transform.createTransformedShape(shape);
shapes.set(activeShape, shape);

如果没有帮助,请提供SSCCE