1

I made a custom cursor using this tutorial. Problem is, as soon as it changes, i just get nothing. The cursor is invisible. I tried the pencil image given there, a custom image i quickly have drawn in paint, but they all don't work.

    public Cursor stoneCursor;
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("pencil.gif");
    Point hotspot = new Point(0,0);
    stoneCursor = toolkit.createCustomCursor(image, hotspot, "Stone");
    getContentPane().setCursor(stoneCursor);

This is inside a JFrame ofcourse.

". If the image to display is invalid, the cursor will be hidden (made completely transparent), and the hotspot will be set to (0, 0)." This is written in the javadoc of createCustomCursor(), but it should work with the pencil.gif?

Thanks for the answers in advance! :)

wlfbck
  • 554
  • 8
  • 22
  • 2
    First off, have you tested that you are loading the image right, that the image variable holds a viable Image object? To test this, I would make an ImageIcon with the Image, put it in a JLabel and display it in a JOptionPane. – Hovercraft Full Of Eels Jul 03 '12 at 12:02
  • I'm pretty sure it works, but will test again. One moment. edit: It looks like it doesn't work... Strange, since i got it from that tutorial. I already googled, but how can i ensure/create a working image? – wlfbck Jul 03 '12 at 12:08
  • @user1433835 Your path to the file is probably not correct. Is you image in the bin folder/root of your application? – brimborium Jul 03 '12 at 12:36

2 Answers2

5

Your code works for me. I am betting that the toolkit can't find your image and therefore is unable to display it. Here is a complete working example that uses the same code as yours (except I used a public Image from a URL):

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestCursor {

    protected void initUI() throws MalformedURLException {
        JFrame frame = new JFrame("Test text pane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = toolkit.getImage(new URL("http://fc03.deviantart.net/fs71/f/2010/094/7/9/Kalyan_hand_cursor_1_by_Zanten.png"));
        Point hotspot = new Point(0, 0);
        Cursor cursor = toolkit.createCustomCursor(image, hotspot, "Stone");
        frame.setCursor(cursor);

        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new TestCursor().initUI();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • tried it with your URL, also doesn't work :( Your test case alone works on the other hand. – wlfbck Jul 03 '12 at 12:16
  • @user1433835 try to use an absolute path to your file: `File cursorFile = new File("C:\\Users\\John\\pencilf.gif"); Image image = toolkit.getImage(file.getAbsolutePath());`. Anyway, this example is working from my station, so unless you don't have Internet access (or you are going through a Proxy), it should work properly. – Guillaume Polet Jul 03 '12 at 12:20
  • Absolute path was the solution! But why? I already tried (by accident) a wrong filename, which instantly threw an exception. Thanks for your help :) – wlfbck Jul 03 '12 at 12:24
  • @brimborium I'm using JDK 1.7 – wlfbck Jul 03 '12 at 12:25
  • @brimborium the issue you are mentionning is related to JDialogs but here I am using a visible JFrame, so even on Java 1.5 it should work (sorry I have no JDK5 to test this) – Guillaume Polet Jul 03 '12 at 12:26
  • @user1433835 eventually this relies on a File. If you provide an absolute path, there is no ambiguity, if you provide an absolute path, then it is calculated against the working directory (the system property "user.dir"). So if you put the pencif.gif image in your working directory, your initial code work as well. – Guillaume Polet Jul 03 '12 at 12:36
  • 1
    Can't edit my previous comment anymore, but of course the second "an absolute path" should be "a relative path". – Guillaume Polet Jul 03 '12 at 14:06
1

If you are using Java 1.5, this SO question might be interesting to you. If you use something newer, this code works perfectly for me as well (just tried it). If the image is not there or can not be accessed correctly, then I experience the same effect as you.

Make sure that the image is loaded correctly.

Community
  • 1
  • 1
brimborium
  • 9,362
  • 9
  • 48
  • 76
  • I lnked it unter tutorial in the first sentence :D – wlfbck Jul 03 '12 at 12:17
  • @user1433835 Oops, my bad. ;) Removed the suggestion. – brimborium Jul 03 '12 at 12:19
  • JDK 1.7, could it be a possible conflict with other elements? There is another JFrame with only a color chooser (which i didn't even activate to simplify testing) and in this JFrame a canvas and two buttons. – wlfbck Jul 03 '12 at 12:21