Change Mouse Cursor linux problem.

Hi all,

Changing the mouse cursor using Java Toolkit does not work because it seems that only a 2-colored image is supported.
I have tested this in many linux installations (with gnome, kde and so on).

I would like to know if some of you can paste the code to render the cursor via software drawing it over the hardware cursor.

Hiding the cursor using a null image works also under linux. But how i can craw the image over the mouse position?

Can you please past some relevant code.

Thanks in advance.
Andrea.

Do you know how to use a MouseListener? Use a MouseListener to get the X and Y location of the mouse cursor, where you can then draw an image at that location.


public class MouseTracker implements MouseListener {
    private int mouseX, mouseY;
    
    public int getMouseX() {
        return mouseX;
    }
    
    public int getMouseY() {
        return mouseY;
    }
    
    public void mouseMoved(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
    }
    
    public void mousePressed(MouseEvent me) {}
    public void mouseReleased(MouseEvent me) {}
}

// to add a listener

MouseTracker mt = new MouseTracker();

myComponent.addMouseListener(mt);

Well usage of listener is clear. What is not clear to me is where to put the drawing code.
Can you add some code that explain the thing?
Thank you for your time.

I think, you could go with hiding the cursor, if inside your window, and drawing your own, at the position of the cursor :slight_smile:
You would have total freedom then.

You would simply draw an image at the mouse location:


g.drawImage(myCursorImage,mt.getMouseX(),me.getMouseY(),null);

If you don’t know how to draw things in Java2D, I suggest you read the official Oracle Tutorials.