Java custom cursor won't work on new computer

自闭症网瘾萝莉.ら 提交于 2020-01-24 22:03:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!