Member Avatar for iamthwee

Does anyone have a sample fully working code, that demonstrates how to change the mouse cursor picture when moved over a certain area in a program?

[edit] just to clarify change it to a mouse cursor I designed myself[/edit]

ThanQ

The Toolkit class provides you with a method createCustomCursor(Image cursor, Point hotspot, String name) . cursor is the Image of the cursor, hotspot is point from the top left corner of the Image where the actual click of the cursor is, and name is just the name of the cursor.

Anyway, here's some compileable code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CustomCursorExample extends JFrame {
    public CustomCursorExample() {
        super("Custom Cursor Example");
        
        JPanel pane = (JPanel) getContentPane();
        pane.setPreferredSize(new Dimension(300, 300));
        
        // change the next three objects to your needs
        final Image cursorImage = new ImageIcon("MyCursor.gif").getImage();
        final Point hotspot = new Point(0, 0);
        final String name = "My Cursor";
        
        JButton mouseButton = new JButton();
        mouseButton.setLocation(10, 10);
        mouseButton.setSize(80, 30);
        mouseButton.setCursor(getToolkit().createCustomCursor(cursorImage, hotspot, name));
        pane.add(mouseButton);
        
        pane.setLayout(new BorderLayout());
        
        pack();
        setVisible(true);
        setResizable(false);
    }
    
    public static void main(String[] args) {
        new CustomCursorExample();
    }
}

Hope this helps.

Member Avatar for iamthwee

Thanx you guys those two helped... I hate reading the java api though I just can't seem to get it to work with an example of my own.... :sad:

@destin

I can't seem to get the cursor to display properly. The picture is kind of distorted, I just assume this is because it is trying to shrink the original picture into a smaller one.

Anyway, shouldn't I be saving the picture with the .cur extension? And how do I achieve transparency?

God bless

I can't seem to get the cursor to display properly. The picture is kind of distorted, I just assume this is because it is trying to shrink the original picture into a smaller one.

Possibly. What size is the image?

Anyway, shouldn't I be saving the picture with the .cur extension?

No. As long as it is an image it will work (.png, .gif, .jpg, etc.).

Member Avatar for iamthwee

Possibly. What size is the image?

No. As long as it is an image it will work (.png, .gif, .jpg, etc.).

So what is the default size supposed to be? Is it defined by pixels?

And How do you achieve transparency? That's the most important question, otherwise it doesn't behave like a normal mouse pointer.

Thanx for the help.

So what is the default size supposed to be? Is it defined by pixels?

I beleive the default size is 32 pixels to 32 pixels. You don't need to adhere to this size though.

And How do you achieve transparency? That's the most important question, otherwise it doesn't behave like a normal mouse pointer.

The image must have have a transparent background.

Member Avatar for iamthwee

>The image must have have a transparent background.

Perhaps a silly question, but how do you do that in M$ paint?

I don't think paint can do transparency. Try The Gimp. Its free and written in java.

Member Avatar for iamthwee

I don't think paint can do transparency. Try The Gimp. Its free and written in java.

Sorrie, all my pics are in paint so i cant use the gimp :sad:

just save the paint picture to something, like 'example.png'.
then go to gimp, or this application i like called 'Paint.net' - not a site, it's actually an application name - and edit it from there, by opening the file you made in paint, in this instance 'example.png'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.