ZeroZipZilch
ZeroZipZilch

Reputation: 721

Yellow circle around cursor in Java

I'm trying to figure out how to make a yellow circle around the cursor in Java. The thing is that we've got a screen-recorder that (obviously) records the screen. Using the keywords "Yellow circle around cursor in Java" on Google only takes me to how to add a yellow circle around the cursor on a MAC, on WM and other applications, but not how to do it in Java on a users computer when the application starts.

How can I do it without using existing images? Drawing a simple yellow circle with some opacity would be the easiest thing to do, making it follow the mouse on the screen. And also, if it is possible to make it disappear and reappear when the user clicks a mouse-button, that'd be awesome.

Upvotes: 2

Views: 2439

Answers (2)

richj
richj

Reputation: 7529

It is possible to do this by attaching a MouseMotionListener to your component, but it will take a little work to get it working exactly how you want it.

I would start from something like this:

private static final double RADIUS    = 15.0;
private static final double DIAMETER  = 2.0 * RADIUS;
private static final Color  XOR_COLOR = Color.yellow;

private static Shape m_circle = null;

@Override
public void mouseMoved(MouseEvent e)
{
    Graphics2D g2     = (Graphics2D) getGraphics();
    Point      p      = e.getPoint();
    Shape      circle = new Ellipse2D.Double(p.getX() - RADIUS, p.getY() - RADIUS, DIAMETER, DIAMETER);

    clearCircle(g2);

    g2.setXORMode(XOR_COLOR);
    g2.draw(circle);
    g2.setPaintMode();

    m_circle = circle;
}

private void clearCircle(Graphics2D g2)
{
    if (m_circle != null)
    {
        g2.setXORMode(XOR_COLOR);
        g2.draw(m_circle);
        g2.setPaintMode();

        m_circle = null;
    }
}

It will also be necessary to make sure that the old circle is cleared on the mouseExited event which you can listen for by adding a MouseListener. This also has the mousePressed/mouseReleased/mouseClicked events that you need for making it disappear/reappear on a user's mouse click.

Using XOR is convenient because it is very easy to restore the screen by repainting the same shape with the same color and style but it isn't quite what you asked for. It is possible to repair the screen by capturing an image of the area that you are about to draw the circle into. The circle can be removed from the screen by repainting the damaged area before painting a circle in a new position.

Upvotes: 2

michael667
michael667

Reputation: 3260

It's not possible to add a circle around the existing mouse pointer. You can only set the mouse pointer to a complete image.

Upvotes: -1

Related Questions