我最近买了一台新计算机,然后将项目从旧的转移到了新的计算机上。我对所有项目都进行了编译,并且它们都运行良好,并且大多数仍在新计算机上运行,但是特别是一个项目不会显示我已移动的自定义光标。我确定我随项目一起移动图片只是为了排除这种情况。我重写了源代码以使其与新计算机上的新位置匹配,但仍不会显示。它给我错误信息:
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)
我的旧计算机是32位,而新计算机是64位,都在Windows 7上运行,我正在使用eclipse Kepler,但是使用Cursor和Toolkit时有关系吗?
这是我用来创建游标的代码
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);
编辑:这是所有想要查看的代码。
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);
}
}
光标热点应相对于光标图像...
可能的原因是给定的x / y坐标超出了图像的可见范围...
Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(cursor,new Point(this.getX(),this.getY()), "cursor");
例如,假设以下光标为32x32像素...
光标的热点将约为26x0,这表示在该鼠标事件将被触发,且所述点Point
的MouseEvent
将被登记为已经发生
另一种可能性是实际上未加载图像...
Image cursor = Toolkit.getDefaultToolkit().getImage("graphx/PNG/cursor.png");
getImage
期望该值表示文件位置,在此示例中,这意味着文件应相对于程序已执行的位置
如果图像实际上是嵌入式资源,则应使用
Image cursor = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("/graphx/PNG/cursor.png"));
或类似图片以加载图片。
您可以使用进行测试,ImageIO.read
因为IOException
如果由于某种原因而无法加载图片,则会抛出一个
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句