javaswingawt

How to move different shapes with MouseListener


I have a program that paints different shapes that I put in the ArrayList, it works fine to iterate with shapes to paint them but my methods for moving them doesn't work. Is it something wrong with my move() method?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MyShapes extends JPanel {
private Point2D.Float position;

private final DifferentShapes[] shapes = new DifferentShapes[]{new Circle(), new Triangle(), new Square()};

MovingAdapter ma = new MovingAdapter();

    public MyShapes() {
        addMouseMotionListener(ma);
        addMouseListener(ma);
    }

    interface DifferentShapes {
        void paint(Graphics2D graphics);
        boolean contains(int x, int y);
        void move(int dx, int dy);
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D graphics = (Graphics2D) g;
    for (DifferentShapes shape : this.shapes) {
        shape.paint(graphics);
    }
}

class MovingAdapter

  public  class MovingAdapter extends MouseAdapter {

    private int x;
    private int y;

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
    }

    public void mouseDragged(MouseEvent e) {
        final int dx = e.getX() - x;
        final int dy = e.getY() - y;

        for (DifferentShapes shape : shapes) {
            if (shape.contains(x, y)) {
                shape.move(dx, dy);
            }
        }
        x += dx;
        y += dy;
    }
}


public static void main(String[] args) {
    JFrame frame = new JFrame("Shapes World");
    MyShapes m = new MyShapes();
    m.setDoubleBuffered(true);
    frame.add(m);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 300);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
}

class Circle

 class Circle implements MyShapes.DifferentShapes {

public Circle() {
}
public static Ellipse2D.Float myCr = new Ellipse2D.Float(10,10, 100, 100);

public void paint(Graphics2D graphics) {
    Graphics2D circle = (Graphics2D) graphics;

    circle.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    circle.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    circle.setColor(new Color(0, 0, 117));
    circle.fill(myCr);
}
@Override
public boolean contains(int x, int y) {
    if (myCr.contains(x, y)) {
    }
    return true;
}
public void move(int dx, int dy) {
    myCr.x += dx;
    myCr.y += dy;
}

}

When I use regular if statement it works fine but not in the for loop and my move() method


Solution

  • The logic is right and you are correctly changing the coordinates. You just need to call repaint() in order to make the the changes display.

    if (shape.contains(x, y)) {
       shape.move(dx, dy);
       repaint();
    }