You can create cursor for
your application using
setCursor(Cursor) method of Component class. You can use some built in variables
(HAND_CURSOR, TEXT_CURSOR, MOVE_CURSOR etc.) to create the Cursor object. But
to create a customized cursor you need to write few lines of Java code.
Suppose you have an image file named
myCursorImage.gif and you
want this image to be used as the cursor of your application- just create the
following classes.
import java.awt.Cursor; import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; public class MyCursor { public static Cursor getMyCursor(String imageFileName) { Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage(imageFileName); Point hotspot = new Point(0, 0); Cursor myCursor = toolkit.createCustomCursor(image, hotspot, "My Cursor"); return myCursor; } }
Now you can use the method
getMyCursor(String) of this class to create your customized cursor where you can use
your own image (gif, jpg or
png). A
testing program is also written for you. You must put all the class files
and the image file in the same directory to run the following code.
import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JTextField; public class CursorTest extends JFrame { public CursorTest() { Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(new JTextField(15)); c.add(new JButton("Click")); setCursor(MyCursor.getMyCursor("myCursorImage.gif")); setBounds(100, 100, 200, 200); setVisible(true); } public static void main(String args[]) { CursorTest ct = new CursorTest(); } }