Take the 2-minute tour ×

Let's say that I have drawn a square on the screen using the following code:

I want that if the user clicks on the shape, a border with four small boxes (north, south, east and west) is drawn on the shape. Hovering on any of the small boxes changes the cursor to the resize cursor and, if the user drags the small box to a new location, the shape is resized.

How can this be done please?

share|improve this question
not your downvoter, but agreed with that, not completed question based on wrong code, container for Graphics/2D must returns PreferredSize, most important rest isn't asked –  mKorbel Nov 21 '12 at 9:44
add comment

1 Answer

up vote 3 down vote accepted

Instead of using drawRect() method create Shape object for the rectangle and draw the Shape. For Shape you can use getBounds() to get main rectangle. Use the bounds to find the small boxes rectangles (also Shapes).

When mouse is moved (or dragged) check whether one of the boxes shapes contains the mouse event coordinates.

On drag you can change original Shape using AffineTransfrorm.

share|improve this answer
I have to use drawRect(). It is one of the requirements of the project. –  Matthew Nov 21 '12 at 9:21
ok, use Rectangles. They are also instances of Shape. –  StanislavL Nov 21 '12 at 9:26
add comment

Your Answer


