GAMES LESSON FOURTEEN: Mouse Cursor

You can change the type of cursor you use in your applet. This might be useful for a number of reasons. Take a look at the cursor options by clicking a few times on the sample applet.

The cursor options are listed below in an array of int values.

int cc[] = { Cursor.CROSSHAIR_CURSOR, Cursor.DEFAULT_CURSOR, Cursor.HAND_CURSOR, Cursor.TEXT_CURSOR, Cursor.WAIT_CURSOR };
You use the following command to load a cursor.
setCursor(new Cursor(cc[i]));

MCursor.java

import java.awt.*; import java.applet.*; import java.awt.event.*; public class MCursor extends Applet{ Dimension d; Image offI; int cc[] = { Cursor.CROSSHAIR_CURSOR, Cursor.DEFAULT_CURSOR, Cursor.HAND_CURSOR, Cursor.TEXT_CURSOR, Cursor.WAIT_CURSOR }; int i; public void init(){ d = getSize(); offI=createImage(d.width,d.height); this.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent m) { setCursor(new Cursor(cc[i])); i++; i%=cc.length; repaint(); } }); } public void update(Graphics g){ paint(g); } public void paint(Graphics g){ Graphics offG = offI.getGraphics(); offG.setColor(Color.yellow); offG.fillRect(0,0,d.width,d.height); offG.setColor(Color.black); offG.drawString("Click to change cursor.", 10, d.height-5); g.drawImage(offI, 0,0, this); } }


ASSIGNMENT: