In the Following example ChangeCursor shows how to Changing the Cursor in Java with setCursor() method defined in the Component class. setCursor method is used to changes the cursor icon when the cursor goes upon the particular component. and the getPredefinedCursor() method returns the cursor object to be specified in the cursor block. Changing Cursor means showing cursor in many different shapes when the cursor goes upon the particular component.
Here is the java code for the program ChangeCursor :
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class ChangeCursor extends Applet{
public static void main(String[] args) {
Frame ChangeCursorApplet = new Frame("Changing the Cursor in Java");
Applet ChangeCursor = new ChangeCursor();
ChangeCursorApplet.add(ChangeCursor);
ChangeCursorApplet.setSize(350, 250);
ChangeCursorApplet.setVisible(true);
// CROSSHAIR_CURSOR, DEFAULT_CURSOR,
// W_RESIZE_CURSOR, WAIT_CURSOR
// SW_RESIZE_CURSOR, TEXT_CURSOR,
// E_RESIZE_CURSOR, HAND_CURSOR
// S_RESIZE_CURSOR,SE_RESIZE_CURSOR,
// NE_RESIZE_CURSOR, NW_RESIZE_CURSOR;
// MOVE_CURSOR, N_RESIZE_CURSOR,
//setCursor method is used to changes the cursor icon
ChangeCursorApplet.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
ChangeCursorApplet.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void paint(Graphics g) {
g.setColor(Color.darkGray);
g.setFont(new Font("Arial",Font.BOLD,14));
g.drawString("Changing the Cursor in Java Example", 50, 40);
g.setFont(new Font("Arial",Font.BOLD,10));
g.drawString("http://ecomputernotes.com", 200, 205);
}
}