Lost Password?

  #1 (permalink)  
Old 12-02-2007, 10:25 AM
gszauer's Avatar   
gszauer gszauer is offline
Programmer
 
Join Date: Nov 2007
Location: Florida
Age: 18
Posts: 113
Rep Power: 3
gszauer is on a distinguished road
Default Custom Cursors using Java

This is more of a sample than a tutorial. I will provide you with the code to create a custom cursor, and explain the funtions used by refrencing the Java Documentation.
Note, for this to work you need an image called cursor.gif in the same directory as your compiled file.

Lets start with the code, and work backwards:
Java5 Code:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. // The classes needed for custom cursor ate in
  5. // java.awt.Toolkit;
  6.  
  7. public class CustomCursor {
  8.   private int final WIDTH = 300, HEIGHT = 100;
  9.   private JButton button;
  10.   private JTextField textfield;
  11.   private JLabel label, label2;
  12.   private JHandle bHandle;
  13.  
  14.   public CustomCursor () {
  15.     JFrame myFrame = new JFrame();
  16.     button = new JButton ("Show me");
  17.     textfield = new JTextField();
  18.     label = new JLabel ("Enter string");
  19.     label2 = new JLabel (" ");
  20.     bHandle = new JHandle();
  21.     button.addActionListener(bHandle);
  22.    
  23.     Toolkit toolkit = Toolkit.getDefaultToolkit();
  24.     Image cursorImage = toolkit.getImage("cursor.gif");
  25.     Point cursorHotSpot = new Point(0,0);
  26.     Cursor customCursor = toolkit.createCustomCursor(cursorImage, cursorHotSpot, "Cursor");
  27.     myFrame.setCursor(customCursor);
  28.    
  29.     Container pane = myFrame.getContentPane();
  30.     pane.setLayout (new GridLayout(2, 2));
  31.     pane.add(label);
  32.     pane.add(label2);
  33.     pane.add(textfield);
  34.     pane.add(button);
  35.    
  36.     myFrame.setSize(WIDTH, HEIGHT);
  37.     myFrame.setTitle("Cursor Frame");
  38.     myFrame.setVisible(true);
  39.     myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40.   }
  41.  
  42.   private class JHandle implements ActionListener {
  43.     public void actionPerformed(ActionEvent e){
  44.       String display = textfield.getText();
  45.       JOptionPane.showMessageDialog(null, display, "MyWindow", JOptionPane.PLAIN_MESSAGE);
  46.     }
  47.   }
  48.  
  49.   public static void main (String[] arg){
  50.     CustomCursor myWindow = new CustomCursor();
  51.   }
  52. }

For now i will focus on the codeblock wich makes the custom cursor possible:
Java5 Code:
  1. Toolkit toolkit = Toolkit.getDefaultToolkit();
  2. Image cursorImage = toolkit.getImage("cursor.gif");
  3. Point cursorHotSpot = new Point(0,0);
  4. Cursor customCursor = toolkit.createCustomCursor(cursorImage, cursorHotSpot, "Cursor");
  5. myFrame.setCursor(customCursor);

-- Line 1
Lets start with the Toolkit:
Toolkit (Java 2 Platform SE v1.4.2)
And look at the getDefaultToolkit function:
Toolkit (Java 2 Platform SE v1.4.2))

-- Line 2
The image class:
Image (Java 2 Platform SE v1.4.2)
And the toolkits get image function:
Toolkit (Java 2 Platform SE v1.4.2))

-- Line 3
The point class:
Java 2 Platform SE v1.3.1: Class Point

-- Line 4
The cursor class:
Java 2 Platform SE v1.3.1: Class Cursor
The create custom cursor function:
Toolkit (Java 2 Platform SE v1.4.2))

-- Line 5
This is self explanatory. Note you do not have to use a JFrame. You could set custom cursors for JButtons, ect...
__________________
Quote:
Originally Posted by ~Aristotle
It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
questions about java games stack Java Help 3 07-02-2007 05:35 PM
MS-SQL deadlock and hang the Java application reachpradeep Database & Database Programming 1 03-11-2007 04:20 AM
Java Facts techni68 Java Help 0 01-17-2007 01:41 PM
John's Java Tutorial Index John Java Tutorials 0 01-11-2007 03:05 PM
Java Help Files xXHalfSliceXx Java Help 3 11-28-2006 11:30 PM


All times are GMT -5. The time now is 10:21 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads