Reputation: 395
In my program in java, I have drawn these polygons in my JFrame and I want my mouse cursor to change to the one that I click on it, so by clicking again, the polygon will be drawn in the point of my click. I don't know how to do that since I don't know how to change my cursor to a polygon?
Upvotes: 1
Views: 323
Reputation: 51535
You want to create a custom cursor. The method to create and register that cursor is toolkit.createCustomCursor. Basically, create an image with an appropriate size, install it and use as needed. Here's a code snippet:
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension dim = kit.getBestCursorSize(48, 48);
BufferedImage buffered = GraphicsUtilities.createCompatibleTranslucentImage(dim.width, dim.height);
Shape circle = new Ellipse2D.Float(0, 0, dim.width - 1, dim.height - 1);
Graphics2D g = buffered.createGraphics();
g.setColor(Color.BLUE);
g.draw(circle);
g.setColor(Color.RED);
int centerX = (dim.width - 1) /2;
int centerY = (dim.height - 1) / 2;
g.drawLine(centerX, 0, centerX, dim.height - 1);
g.drawLine(0, centerY, dim.height - 1, centerY);
g.dispose();
Cursor cursor = kit.createCustomCursor(buffered, new Point(centerX, centerY), "myCursor");
Upvotes: 2