问题
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);
}
}
回答1:
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...
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
来源:https://stackoverflow.com/questions/23902159/java-custom-cursor-wont-work-on-new-computer