Why when I set an image to the cursor, it doesn't work?
I suspect you misinterpreted the second argument of 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
The hotspot
is meant relative to the top-left corner of the cursor image,
not to the top-left corner of the panel.
So, instead of
new Point(mainPanel.getX(), mainPanel.getY())
just try
new Point(0, 0)
This MCVE works here, though it shrinks the cursor down to a smaller size.
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);
}
}