为什么当我将图像设置到光标时,它不起作用?

Why when I set an image to the cursor, it doesn't work?

我是 Java 的新手,尝试将图像设置为光标时遇到问题。我正在使用 BufferedImageGraphics.drawImage 但它只绘制图像的颜色而不是完整的 png 图像。

这是我的代码:

/*The images List*/
iconsBet.add(ImageIO.read(getClass().getResource("/resources/ChipType"+ String.valueOf(maxChipBet+1) +".png")));
/*The images List*/

BufferedImage output = new BufferedImage(iconsBet.get(0).getWidth(), iconsBet.get(0).getHeight(), BufferedImage.TYPE_INT_ARGB );
Graphics graphicsCursorIcon = output.getGraphics();

int count = 0;
for(BufferedImage icon : iconsBet)
{                
   graphicsCursorIcon.drawImage(icon, 0, count*10, null);
   count++;
}

graphicsCursorIcon.dispose();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor c = toolkit.createCustomCursor(output , new Point(mainPanel.getX(), mainPanel.getY()), "img");
mainPanel.setCursor(c);

图片:This is one image from the group of images that I'm using

程序只画了一个红色圆圈,没有画出png图像。

我已经尝试使用所有 BufferedImage 类型,但仍然不起作用。 你能帮我解决这个问题吗?我需要做什么才能让它发挥作用?

我怀疑你误解了 Toolkit.createCustomCursor(Image cursor, Point hotSpot, String name) 的第二个参数:

hotSpot - the X and Y of the large cursor's hot spot; the hotSpot values must be less than the Dimension returned by getBestCursorSize

hotspot表示相对于光标图像的左上角, 而不是面板的左上角。 所以,而不是

new Point(mainPanel.getX(), mainPanel.getY())

试试

new Point(0, 0)

这个 MCVE 在这里工作,虽然它将光标缩小到更小的尺寸。

import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;

public class CustomImageCursor {

    private JComponent ui = null;

    CustomImageCursor() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(40, 400, 40, 40));

        try {
            BufferedImage bi = ImageIO.read(
                    new URL("https://i.stack.imgur.com/b89MA.png"));
            Toolkit toolkit = Toolkit.getDefaultToolkit();
            Cursor c = toolkit.createCustomCursor(bi, new Point(0, 0), "img");
            ui.setCursor(c);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                CustomImageCursor o = new CustomImageCursor();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}