• home
  • forum
  • my
  • kt
  • download
  • Using Custom Cursors in Java

    Author: 2007-08-03 13:33:51 From:

    This tutorial shows how to go beyond the predefined cursors in Java and create your own cursors using a GIF or PNG that are dsiplayed when the user moves the mouse over a Java component (AWT or Swing).

    Creating the Cursor Image
    For the custom cursor, a transparent GIF or PNG will be needed for the cursor. For this tutorial, a sample transparent GIF image of a pencil is provided below (right click to save):

    Loading the Cursor Image
    The image for the custom cursor needs to be loaded into an Image object.

            //Get the default toolkit
            Toolkit toolkit = Toolkit.getDefaultToolkit();
    
            //Load an image for the cursor
            Image cursorImage = toolkit.getImage("pencil.gif");

    Defining the Cursor Hot Spot
    The cursor hot spot is used for the point location in mouse events. For a cursor such as a cross-hair cursor, the hot spot would be in the center of the cursor. In our example, the cursor hot spot will be at the pencil point or the point 0,0.

            //Create the hotspot for the cursor
            Point cursorHotSpot = new Point(0,0);
    Creating the Custom Cursor
    To create the custom image, we put together the cursor image and the hot spot:
            //Create the custom cursor
            Cursor customCursor = toolkit.createCustomCursor(cursorImage, cursorHotSpot, "Pencil");

    Displaying the Custom Cursor
    The final step is to notify the component to display the cursor.

            //Use the custom cursor
            setCursor(customCursor);
    

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Applet Building (2)
      Application Building (3)
      Communication (1)
      Database Related (8)
      Development (12)
      EJB (14)
      Game Programming (2)
      General Java (38)
      Javabeans (4)
      JSP and Servlets (8)
      Miscellaneous (23)
      Networking (1)
      Security (2)
      Swing (13)
      WAP and WML (1)
      XML and Java (0)

    New

    Hot