It can be useful in designing a user interface to control the look
of the cursor according to the current position and/or processing
going on.
The AWT has the Cursor
class that comes with 14 different cursor icons.
The Component
class includes the setCursor()
method that can change the cursor icon when it is above a particular
component.
The applet below will display the 14 standard icons above the buttons.
CursorTestApplet
|
import
javax.swing.*;
import java.awt.*;
/** Demonstrate the different cursor styles.**/
public class CursorTestApplet extends JApplet
{
CursorPanel fCursorPanel = null;
/** Build the interface.**/
public void init () {
Container content_pane = getContentPane
();
// Create an instance of a JPanel
sub-class
fCursorPanel = new CursorPanel ();
// And add one or more panels to
the JApplet panel.
content_pane.add (fCursorPanel);
}
} // class CursorTestApplet
/** A JPanel subclass showing the different cursors.**/
class CursorPanel extends JPanel
{
CursorPanel () {
setLayout (new GridLayout (7,2));
JButton bt = new JButton ("Default");
bt.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
add (bt);
bt = new JButton ("Busy");
bt.setCursor (new Cursor (Cursor.WAIT_CURSOR));
add (bt);
bt = new JButton ("Hand");
bt.setCursor (new Cursor (Cursor.HAND_CURSOR));
add (bt);
bt = new JButton ("Text");
bt.setCursor (new Cursor (Cursor.TEXT_CURSOR));
add (bt);
bt = new JButton ("CrossHair");
bt.setCursor (new Cursor (Cursor.CROSSHAIR_CURSOR));
add (bt);
bt = new JButton ("Move");
bt.setCursor (new Cursor (Cursor.MOVE_CURSOR));
add (bt);
bt = new JButton ("East Resize");
bt.setCursor (new Cursor (Cursor.E_RESIZE_CURSOR));
add (bt);
bt = new JButton ("North Resize");
bt.setCursor (new Cursor (Cursor.N_RESIZE_CURSOR));
add (bt);
bt = new JButton ("West Resize");
bt.setCursor (new Cursor (Cursor.W_RESIZE_CURSOR));
add (bt);
bt = new JButton ("South Resize");
bt.setCursor (new Cursor (Cursor.S_RESIZE_CURSOR));
add (bt);
bt = new JButton ("NorthEast Resize");
bt.setCursor (new Cursor (Cursor.NE_RESIZE_CURSOR));
add (bt);
bt = new JButton ("NorthWest Resize");
bt.setCursor (new Cursor (Cursor.NW_RESIZE_CURSOR));
add (bt);
bt = new JButton ("SouthWest Resize");
bt.setCursor (new Cursor (Cursor.SW_RESIZE_CURSOR));
add (bt);
bt = new JButton ("SouthEast Resize");
bt.setCursor (new Cursor (Cursor.SE_RESIZE_CURSOR));
add (bt);
} // ctor
} // class CursorPanel
|
With Java 1.2 came the ability to create custom cursor
icons. The method
createCustomCursor(Image
cursor, Point hotSpot, String name)
in the java.awt.Toolkit
class, you pass the image for the cursor. The second argument specifies
the so-called hotspot that sets the X, Y coordinates, relative
to the top left corner of the image, of the pixel where the click
occurs. For example, this would specify the end of the tip of an
arrow cursor. The last argument provides the name of the cursor
for the Java
Accessibility system to use. (The Accessibility framework, not
discussed here, provides for enhancements for handicapped users.)
References & Web Resources
Latest update: Dec. 6, 2004
|