1

I recently bought a new computer and I moved my projects from my old one to my new one. I did a compilation on all of my projects and they all worked fine, and most of them still do on my new computer, but one project in particular wouldn't display the custom cursor that I had moved. I made sure that I moved the picture with the project just to rule that out. I rewrote the source to match the new location on my new computer, but it still won't display. It gives me the error message:

Exception in thread "main" java.lang.IndexOutOfBoundsException: invalid hotSpot
    at sun.awt.CustomCursor.<init>(Unknown Source)
    at sun.awt.windows.WCustomCursor.<init>(Unknown Source)
    at sun.awt.windows.WToolkit.createCustomCursor(Unknown Source)
    at wtalfvn.Window.<init>(Window.java:32)
    at wtalfvn.Main.main(Main.java:9)

My old computer is a 32 bit and my new one is a 64 bit, both run on Windows 7, I am using eclipse Kepler, but does it matter when using the Cursor and Toolkit?

Here is my code I used to create my Cursor

Image cursor = Toolkit.getDefaultToolkit().getImage("graphx/PNG/cursor.png");
Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");
this.setCursor(c);

EDIT: Here is the whole code for those who want to see it.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class Window extends JFrame{

Image ico= Toolkit.getDefaultToolkit().getImage("graphx/ico/icon.PNG");

TextBox tb=new TextBox();

public Window(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800,600);
    setVisible(true);
    setFocusable(true);
    getContentPane().setBackground(Color.BLACK);
    setIconImage(ico);
    setLocationRelativeTo(null);
    setResizable(false);
    setTitle("MYTITLE");
    addKeyListener(new KeyAdapter(){
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar()==KeyEvent.VK_ESCAPE){
                System.exit(0);
            }
        }
    });
    Image cursor = Toolkit.getDefaultToolkit().getImage( getClass().getResource("/graphx/PNG/cursor.png"));
    Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");
    setCursor(c);
}
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Garrett
  • 17
  • 5
  • Welcome to Stack Overflow! Consider providing an actual [runnable example that demonstrates your problem](https://stackoverflow.com/help/mcve) would involve less guess work and better responses – MadProgrammer May 28 '14 at 03:10
  • All of the other code works, so I decided not to bug you guys to much by copying all my code just to get to the culprit. The window works, the custom icon works, everything works except the cursor. – Garrett May 28 '14 at 03:14
  • The problem is, I'm trying to guess what `this.getX` and `this.getY` mean, to me, they mean component locations, but that might not be true – MadProgrammer May 28 '14 at 03:18
  • I edited it, so does that clear things up a bit better? – Garrett May 28 '14 at 03:19
  • Yes, `getX` and `getY` are not suitable methods to use for the hot spot of the cursor in this context – MadProgrammer May 28 '14 at 03:19
  • All I know is that it worked on the 32 bit, no edited code, but now it doesn't work on my new computer... – Garrett May 28 '14 at 03:20
  • I can see two reasons why it wouldn't work (and at least one which will be problematic). Because different systems/OS operating differently (even two machines running the same OS will operate slightly differently depending on what they are doing), you will get variable results. – MadProgrammer May 28 '14 at 03:29

1 Answers1

0

The cursor hot spot should relative to the cursor image...

The likely cause is the fact that the given x/y coordinates are outside the visible range of the image...

 Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");

For example, assuming the following cursor is 32x32 pixels...

CursorExample

The cursor hot spot would be around 26x0, this represents the point at which mouse events would be triggered and the Point the MouseEvent would be registered as having occured

The other possibility is that the image is actually not been loaded...

Image cursor = Toolkit.getDefaultToolkit().getImage("graphx/PNG/cursor.png");

getImage expects that the value represent a file location, which in this example, means the file should be relative to the location that the program is been executed

If the image is actually an embedded resource, you should be using

Image cursor = Toolkit.getDefaultToolkit().getImage(
    getClass().getResource("/graphx/PNG/cursor.png"));

or simular to load the image.

You can test this by using ImageIO.read as this will throw an IOException if the image can't be loaded for some reason

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Ok, well I got rid of the getX() and getY() parameters and it still does the same thing... I did the getClass().getResource() and still, nothing. From examining my code (And that is all the code by the way other than a Main class where I gather all my constructors in the main function) what can you tell that needs to be fixed? – Garrett May 28 '14 at 03:30
  • Where is the image stored? Is it within the context of the application Jar or is stored externally on the file system – MadProgrammer May 28 '14 at 03:31
  • Sorry to be such an annoying newb. I wish I had never put this down now because I am making myself look like a total idiot :(. – Garrett May 28 '14 at 03:32
  • Welcome to my club, the memebership is huge – MadProgrammer May 28 '14 at 03:33
  • It is stored as a resource file in eclipse. I used the same basic path for my icon and it works just fine. – Garrett May 28 '14 at 03:33
  • As a resource, it should be loaded using `Class#getResource`. Have you looked into the Jar file to see if was included? Or the path is what you are expecting? Try using `ImageIO.read` instead – MadProgrammer May 28 '14 at 03:35
  • I think the Image is loading because I try taking out the G at the end of PNG and the cursor just disappears all together. I will try to see if that works. Thanks – Garrett May 28 '14 at 03:36
  • Ok, it just starting working for no particular reason... Thanks for your time. This is just WEIRD........ – Garrett May 28 '14 at 03:38