Gigi Bayte 2
Gigi Bayte 2

Reputation: 984

Hide Java cursor at any place on the screen

I wish to hide a cursor in a certain range of x and y positions. Here is some sample code that represents what I want to do.

if(x >= xLowerBound && x <= xUpperBound + 600 && y >= yLowerBound + 20 && y <= yUpperBound + 600)
    setCursor(blankCursor);
else
    setCursor(Cursor.getDefaultCursor());

Now, I know that setCursor() can be applied to a certain object, and that is fine. However, that doesn't work for my purposes.

The only exception would be if I could somehow create a fullscreen invisible object in which I could use setCursor even though it's invisible like so:

JFrame hiddenWindow = new JFrame();
hiddenWindow.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());
hiddenWindow.setExtendedState(JFrame.MAXIMIZED_BOTH);

if(x >= xLowerBound && x <= xUpperBound && y >= yLowerBound && y <= yUpperBound)
    hiddenWindow.setCursor(blankCursor);
else
    hiddenWindow.setCursor(Cursor.getDefaultCursor());

(Note: This did not work.)

Just reiterating, I need to be able to use setCursor() anywhere on the screen, not just limited to one object unless I can create an invisible screen-sized object to use setCursor() in.

Update 1:

I suppose I could have been a little more clear with what I was doing. I have a main JFrame in the center of the screen. It does not take up the whole screen. I am setting the cursor to a blank cursor whenever it is in a certain distance away from the JFrame or inside the JFrame itself. However, I do not know how I would do that.

My idea was to perhaps use another JFrame that takes up the whole screen and is invisible behind it so that I can use setCursor() on that JFrame. for the space outside it. I hope this clarifies the question a bit more.

Upvotes: 0

Views: 1172

Answers (1)

user1803551
user1803551

Reputation: 13427

You can use a MouseMotionListener and override its mouseMoved method to check where the mouse is (in the components) and set the cursor type accordingly:

public class Test extends JFrame {

    Test() {
        final int x1 = 100, y1 = 100, x2 = 300, y2 = 300;

        BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
        Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);

        addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseMoved(MouseEvent e) {
                int x = e.getX();
                int y = e.getY();
                if (x > x1 && x < x2 && y > y1 && y < y2)
                    setCursor(blankCursor);
                else
                    setCursor(defaultCursor);
            }
        });

        setSize(new Dimension(400, 400));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Test());
    }
}

Credit to this answer for the trick of a blank cursor.

You cannot set the cursor outside of a Java window. You can go with your idea of an invisible, fullscreen, headless frame, but this is pretty dodgy and will also intercept all mouse events. Implementation will be the same.

Upvotes: 1

Related Questions