Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I move two shapes at the same time while by dragging one shape?
    text
    copied!<p>On my canvas, I have Circle A and Circle B. Circle A is to be the designated parent to Circle B. What I'm trying to develop is a Parent-Child relationship between the two shapes. So if Circle A is dragged 50 units on the X-axis and 25 units on the Y-Axis, Circle B will also move as such. But if Circle B is dragged, Circle A stays in its last position.</p> <p>The problem I'm having is re-dragging Circle A. From both circles, initial position, dragging Circle A will move Circle B accordingly. But once I release the mouse and try to drag Circle A again, Circle B moves x-amount of units more than Circle A's x-position and y-amount of units more than Circle A's y-position. So each time that I re-click and drag Circle A, Circle B increments to a new position on it's own x and y axes at a further distance each time the mouse button is pressed. </p> <pre><code>// initial positions of Circles (Part of Circle class which draws two circles) int parentCirclePosX = 100; int parentCirclePosY = 100; int childCirclePosX = 103; int childCirclePosY = 143; public void mouseInput() { getCanvas().addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent event) { super.mouseReleased(event); _cir.isDraggable = false; Main.parentLabel.setText("Released " + _cir.isDraggable); } @Override public void mousePressed(MouseEvent event) { super.mousePressed(event); // Original mouse coordinates before dragging last_x = _cir.getParentCircleX() - event.getX(); last_y = _cir.getParentCircleY() - event.getY(); // Child Circle's last position lastChildx = _cir.getChildCircleX(); lastChildy = _cir.getChildCircleY(); int button = event.getModifiers(); // Check if mouse pointer is hovering over Parent Circle boolean inXBounds = event.getX() &gt; _cir.getParentCircleX() &amp;&amp; event.getX() &lt; _cir.getParentCircleX() + _cir.PARENT_CIRCLE_SIZE; boolean inYBounds = event.getY() &gt; _cir.getParentCircleY() &amp;&amp; event.getY() &lt; _cir.getParentCircleY() + _cir.PARENT_CIRCLE_SIZE; if (button == InputEvent.BUTTON1_MASK &amp;&amp; inXBounds &amp;&amp; inYBounds) { _cir.isDraggable = true; Main.parentLabel.setText("Pressed " + _cir.isDraggable); } getCanvas().repaint(); } }); getCanvas().addMouseMotionListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent event) { super.mouseDragged(event); if (_bone.isDraggable) { updateLocation(event); Main.parentLabel.setText(" Parent X: " + _cir.getParentCircleX() + " Y: " + _cir.getParentCircleY()); Main.childLabel.setText("Child X: " + _cir.getChildCircleX() + " Y: " + _cir.getChildCircleY()); } getCanvas().repaint(); } }); } private void updateLocation(MouseEvent e) { _cir.setParentCircleX(last_x - _cir.PARENT_CIRCLE_SIZE / 2); _cir.setParentCircleY(last_y - _cir.PARENT_CIRCLE_SIZE / 2); _cir.setChildCircleX(lastChildx + e.getX() - 108 ); _cir.setChildCircleY(lastChildy + e.getY() - 108); last_x = e.getX(); last_y = e.getY(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload