1

Figure showing two rectangles which have few other rectangles on them

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?

Rookie86
  • 59
  • 4
  • 1
    Sure. Use a translate instance of an `AffineTransform` when drawing at the new position. – Andrew Thompson Mar 22 '14 at 09:58
  • 1
    Yes, 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 Answers1

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045