Search code examples
javaswingcursor

Using a custom cursor


I've searched quite a few threads here but none have helped.

I have a few JFrames and each JFrame should have its own individual cursor, symbolizing which version of the program the user is using. These files are in /AndroidToolkit/resources. The files are all .cur files, so they're actually proper cursors and not just images.

I've tried a few methods of doing this, but I have succeeded. I've tried using ImageIO, Toolkit, and my last try was:

Cursor cCur = Toolkit.getDefaultToolkit().createCustomCursor(getClass().getResource("../resources/ImpressionCursor.cur").getFile()., null, null);

How can I do this in an easy way, which is easy for other people to understand, without me having to always comment it with 10k lines?

Thanks in advance, Beats


Solution

  • This is what you need, try this code 100% works for me on Ubuntu 12.04 LTS, it should work for you too:

    public static void main(String[] args) {
    
            Toolkit toolKit = Toolkit.getDefaultToolkit();
            Image pencil = toolKit.getImage("pencil.gif");
            Cursor cursor = toolKit.createCustomCursor(pencil, new Point(0, 0), "Pencil");
    
            JFrame frame = new JFrame("Cursor Test");
            frame.setSize(300, 300);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
    
            frame.setCursor(cursor);
            frame.setVisible(true);
    
        }
    

    this is the link for the gif file click here, I hope that helps, Salam