Jump to content




Recent Status Updates

  • Photo
      18 Aug
    KodeKool

    When faced with a wall of errors and no hope to fix them, remember the following "Programs always do what you tell them to, and seldom what you want them to, but eventually you'll run out of things that can go wrong and it'll just work. and that's the secret to good programming."

    Show comments (2)
  • Photo
      11 Aug
    Error

    Should I be practicing programming every day? I feel if I don't, I'll get instantly rusty or something.

    Show comments (4)
View All Updates

Developed by Kemal Taskin
Photo
- - - - -

Custom Cursors using Java


  • Please log in to reply
No replies to this topic

#1 gszauer

gszauer

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 112 posts

Posted 02 December 2007 - 08:25 AM

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:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// The classes needed for custom cursor ate in
// java.awt.Toolkit;

public class CustomCursor {
private int final WIDTH = 300, HEIGHT = 100;
private JButton button;
private JTextField textfield;
private JLabel label, label2;
private JHandle bHandle;

public CustomCursor () {
JFrame myFrame = new JFrame();
button = new JButton ("Show me");
textfield = new JTextField();
label = new JLabel ("Enter string");
label2 = new JLabel (" ");
bHandle = new JHandle();
button.addActionListener(bHandle);

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image cursorImage = toolkit.getImage("cursor.gif");
Point cursorHotSpot = new Point(0,0);
Cursor customCursor = toolkit.createCustomCursor(cursorImage, cursorHotSpot, "Cursor");
myFrame.setCursor(customCursor);

Container pane = myFrame.getContentPane();
pane.setLayout (new GridLayout(2, 2));
pane.add(label);
pane.add(label2);
pane.add(textfield);
pane.add(button);

myFrame.setSize(WIDTH, HEIGHT);
myFrame.setTitle("Cursor Frame");
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class JHandle implements ActionListener {
public void actionPerformed(ActionEvent e){
String display = textfield.getText();
JOptionPane.showMessageDialog(null, display, "MyWindow", JOptionPane.PLAIN_MESSAGE);
}
}

public static void main (String[] arg){
CustomCursor myWindow = new CustomCursor();
}
}


For now i will focus on the codeblock wich makes the custom cursor possible:
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image cursorImage = toolkit.getImage("cursor.gif");
Point cursorHotSpot = new Point(0,0);
Cursor customCursor = toolkit.createCustomCursor(cursorImage, cursorHotSpot, "Cursor");
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...
  • 0

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 Posted Image