xefore
xefore

Reputation: 13

Edit Shape location using mouse

I would like to move Shape objects, which I created previously. I cannot figure out how to change (update) shape location. Now I am getting cast error:

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

Now I can access particular shape, but it seems it doesn't have setLocation() or something like that.

Please give me tip, because I got stuck.

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

Upvotes: 1

Views: 60

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

You haven't provided SSCCE (your code cannot be compiled by me) so I cannot test my solution, but your approach in the method mouseReleased is wrong. To translate the shape you need to replace

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

by

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

If it doesn't help please provide a SSCCE.

Upvotes: 1

Related Questions