How to create a custom cursor in java

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to create a custom cursor in java
You can create a custom black cursor.

By default, your Java application will use the same set of mouse cursors as the underlying operating system. So, on a Windows computer, the Java application will use the current Windows mouse cursor, under Mac OS X, it will use the default Mac OS X mouse cursor, and so on. If you prefer to manually create your own mouse cursors for your application, you can do so with the built-in java.awt.Cursor class.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • Graphics file of the cursor design you want

Show MoreHide

Instructions

  1. 1

    Create a new Java Swing window. If you are using a Java Integrated Development Environment (IDE), like Netbeans or Eclipse, you can take a shortcut in doing this by clicking "File," "New Project," and selecting "Java Application" from the "New Project" dialogue. If you prefer to work the old fashioned way in a text editor, the process of creating it manually is not very involved. Just save a new file with the name "CustomCursorExample.java."

  2. 2

    Import the libraries you will need to build the program into your new application by pasting these at the top of the "CustomCursorExample.java" file:

    import java.awt.Cursor;

    import java.awt.Image;

    import java.awt.Point;

    import java.awt.Toolkit;

    import javax.swing.JPanel;

    Cursor, of course, will represent the cursor itself in the program. Image will hold an image that will represent the cursor, while Toolkit is a helper class that can transform an Image object into a Cursor object. Finally, JPanel is the class that represents an application window in Java.

  3. 3

    Paste the following code into the file (if you are using an IDE, some of this may have already been done for you):

    public class CustomCursorExample extends JPanel {

    public CustomCursorExample() {
    
    
    
        Toolkit tk = Toolkit.getDefaultToolkit();
    
        Image cursor Image = tk.getImage("cursor.gif");
    
    
    
        Cursor cursor = tk.createCustomCursor(cursor Image, new Point(0,0), "Custom Cursor");
    
        this.setCursor(cursor);
    
        this.setSize(640,480);
    
        this.setVisible(true);
    
    }
    
    
    
    public static void main() {
    
        new CustomCursorExample();
    
    }
    

    }

    The line "Cursor cursor = tk.createCustomCursor(cursor Image, new Point(0,0), Custom Cursor");" is the heart of the program. The toolkit (tk) creates a new cursor object using the image and assigns it the name "Custom Cursor." The "Point" is an important argument. This describes the hot spot for the cursor (which pixel in the cursor will perform the click). In this case, the hot spot is the top-left corner of the image, and the graphics for the cursor should be designed accordingly. For example, an arrow pointing to the top-left corner. On the other hand, a crosshair cursor might want to choose a hot spot in the centre of the image.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.