Download: jar file
See page 498 of text for information about loading resources (such as images) from jar files.
CursorTest.java
01: import java.awt.*; 02: import java.awt.event.*; 03: import java.net.*; 04: import javax.swing.*; 05: 06: 07: class TestPanel extends JPanel implements ItemListener 08: { 09: JPanel top; 10: JComboBox cursors; 11: Cursor custom; 12: 13: TestPanel() 14: { 15: //Create and load the duke icon. 16: ImageIcon icon = new ImageIcon(getClass().getResource("dukeWaveRed.gif")); 17: custom = Toolkit.getDefaultToolkit().createCustomCursor(icon.getImage(), new Point(15,15), 18: "Duke Waving"); 19: 20: String names[] = new String[15]; 21: int i; 22: for (i=0; i<20; i++) { 23: try { 24: names[i] = Cursor.getPredefinedCursor(i).getName(); 25: } 26: catch(IllegalArgumentException e) 27: { 28: break; 29: } 30: } 31: System.out.println("There are " + i + " predefined cursors"); 32: names[14] = custom.getName(); 33: cursors = new JComboBox(names); 34: cursors.addItemListener(this); 35: 36: top = new JPanel(); 37: top.add(cursors); 38: } 39: 40: public void itemStateChanged(ItemEvent e) 41: { 42: int idx = cursors.getSelectedIndex(); 43: setCursor( idx<14? Cursor.getPredefinedCursor( idx ): custom ); 44: } 45: 46: } 47: 48: 49: public class CursorTest extends JApplet 50: { 51: public void init() 52: { 53: TestPanel panel = new TestPanel(); 54: add(panel); 55: add(panel.top,BorderLayout.NORTH); 56: } 57: 58: public static void main( String args[] ) 59: { 60: TestPanel panel = new TestPanel(); 61: JFrame frame = new JFrame("Cursor Test" ); 62: frame.add(panel); 63: frame.add(panel.top,BorderLayout.NORTH); 64: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 65: frame.setSize( 240, 180 ); 66: frame.setVisible(true); 67: } 68: }
CursorList.java
01: import java.awt.Cursor; 02: 03: 04: 05: public class CursorList extends Cursor 06: { 07: CursorList() 08: { 09: super(Cursor.DEFAULT_CURSOR); 10: } 11: 12: public static void listCursors() 13: { 14: int n = predefined.length; 15: for (int i=0; i<n; i++) { 16: System.out.println(predefined[i].getName() + "\t" + predefined[i].getType() ); 17: } 18: } 19: }
Maintained by John Loomis, updated Fri Mar 28 16:00:48 2008