【问题标题】:Java custom cursor won't work on new computerJava 自定义光标在新计算机上不起作用
【发布时间】:2014-05-29 01:51:09
【问题描述】:

我最近买了一台新电脑,并将我的项目从旧电脑移到了新电脑。我对我的所有项目进行了编译,它们都运行良好,其中大多数仍然在我的新计算机上运行,​​但特别是一个项目不会显示我移动的自定义光标。我确保我将图片与项目一起移动只是为了排除这种情况。我重写了源以匹配我的新计算机上的新位置,但它仍然不会显示。它给了我错误信息:

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);
}
}

【问题讨论】:

  • 欢迎来到 Stack Overflow!考虑提供一个实际的runnable example that demonstrates your problem 将涉及更少的猜测工作和更好的响应
  • 所有其他代码都可以正常工作,所以我决定不通过复制我的所有代码来打扰你们,只是为了找到罪魁祸首。窗口有效,自定义图标有效,除光标外一切正常。
  • 问题是,我试图猜测 this.getX 和 this.getY 的含义,对我来说,它们意味着组件位置,但这可能不是真的
  • 我编辑了它,这样清楚一点吗?
  • 是的,getX 和 getY 在这种情况下不适合用于光标的热点

标签: java eclipse mouse-cursor


【解决方案1】:

光标热点应该相对于光标图像...

可能的原因是给定的 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

【讨论】:

  • 好的,我去掉了 getX() 和 getY() 参数,它仍然做同样的事情......我做了 getClass().getResource() ,但仍然没有。通过检查我的代码(除了我在 main 函数中收集所有构造函数的 Main 类之外的所有代码)你能看出哪些需要修复?
  • 图像存储在哪里?是在应用程序 Jar 的上下文中还是外部存储在文件系统中
  • 很抱歉成为这么烦人的新手。我希望我现在从来没有放下这个,因为我让自己看起来像个白痴:(。
  • 欢迎加入我的俱乐部,会员人数众多
  • 在eclipse中存储为资源文件。我为我的图标使用了相同的基本路径,它工作得很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 2020-11-29
相关资源
最近更新 更多