• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

how to custom a cursor

as the title.
0
Arzu
Asked:
Arzu
  • 2
1 Solution
 
yorenCommented:
The answer is yes.
0
 
ArzuAuthor Commented:
the question is how?
thank you.
0
 
raid999Commented:
Creating the custom cursor is done through a method in java.awt.Toolkit API:

public Cursor createCustomCursor(Image cursor, Point hotSpot, String name) throws IndexOutOfBoundsException

Toolkit is an abstract class, so first get the native implementation:
        Toolkit tk = panel.getToolkit();
Then create the cursor:
    Cursor cursor = tk.createCustomCursor(img, hotSpot, name);
Now, we can set the cursor on the panel:
     panel.setCursor(cursor);

The Full source Code:

import java.awt.*;
import java.applet.*;

public class CursorApplet extends Applet {


    public void init() {

        //load the image with the Media Tracker
        MediaTracker tracker = new MediaTracker(this);
        Image cursor = getImage(getCodeBase(), "music_note.gif");
        tracker.addImage(cursor, 0);

        try {
            tracker.waitForID(0);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }

        Cursor cr = null;
        //get the toolkit for this environment
        Toolkit tk = getToolkit();

        try

            //this is the x,y coordinates of the image which
            //will actually do the "clicking"
            Point hotSpot = new Point(1, 1);

            //create the custom cursor
            cr = tk.createCustomCursor(cursor, hotSpot, "music_note");
        } catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        //set the cursor for this applet component
        setCursor(cr);
    }
}



0
 
ArzuAuthor Commented:
thank you raid999.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now