Nitz
Nitz

Reputation: 1726

How to draw a rectangle around the mouse cursor in Java?

I had made one module in my project on which user can draw anything using a pencil.

Now I want to create an eraser for that drawing module, so I need it so that as soon as the user clicks on that eraser button then around my mouse cursor I want a little rectangle shape around it, so the user can use it to erase some parts in the drawing.

How to do this? Any suggestions?

Upvotes: 4

Views: 2574

Answers (1)

stacker
stacker

Reputation: 68942

Instead of drawing a rectangle around the cursor, I would suggest to set a custom curser for the selected tool.

Just to show the API, (not tested) something like that sould work.

Image cursorImg = new ImageIcon("rectangle.gif").getImage();        
Point hotspot = new Point(0, 0);     // should be set to the center of your rectangle    
Cursor cursor = getToolkit().createCustomCursor(cursorImg, hotspot, "cursorname");

YourComponent.setCursor( cursor );

EDIT:

I have to add that getToolkit() is a method of java.awt.Component

Upvotes: 6

Related Questions