import javax.swing.JPanel; import java.awt.event.*; import java.awt.event.MouseAdapter; import java.awt.*; import java.awt.geom.Point2D; public class MouseListener extends MouseAdapter { private MyWorld world; private PhysicsElement selectedElement; private Point2D.Double pos; public MouseListener (MyWorld w){ world = w; pos = new Point2D.Double(0,0); } public void mousePressed(MouseEvent e) { MyWorldView.SPACE_INVERSE_TRANSFORM.transform(e.getPoint(),pos); // convert pixel to MyWorld coordenates selectedElement = world.findElement(pos.getX(), pos.getY()); if (selectedElement != null){ selectedElement.setSelected(); world.getView().repaint(); } } public void mouseDragged(MouseEvent e) { if (selectedElement == null) return; Point2D.Double p = new Point2D.Double(0,0); MyWorldView.SPACE_INVERSE_TRANSFORM.transform(e.getPoint(),p); selectedElement.dragDelta(p.getX()-pos.getX(), p.getY()-pos.getY()); pos = p; world.getView().repaint(); } public void mouseReleased(MouseEvent e) { if (selectedElement != null) { selectedElement.setReleased(); world.getView().repaint(); } return; } }