I would like to draw rectangles on another rectangle as shown in the figure. So, when I move the rectangle1 or rectangle2, the other rectangles should also move with it. Is there a way to draw rectangles in this way?
Asked
Active
Viewed 49 times
1
-
1Sure. Use a translate instance of an `AffineTransform` when drawing at the new position. – Andrew Thompson Mar 22 '14 at 09:58
-
1Yes, you can do this. You would use a JPanel as a canvas. In your code, you would have to determine whether the rectangle you draw is inside another rectangle or not. I would create a RectangleObject class, using a Rectangle instance to hold the rectangle parameters. If the rectangle is inside another rectangle, you'd have to modify the x and y position of the inner rectangle in accordance with the x and y position of the outer rectangle. – Gilbert Le Blanc Mar 22 '14 at 10:45
1 Answers
1
Assuming that the outer rectangle represents a selection rectangle meant to select the enclosed rectangles, you can maintain a List<Rectangle>
and update the position of each rectangle as the mouse is dragged. A complete example using List<Node>
is cited here.