Okay, so, this is probably a good indication that I need to go back to bed
Image welcomePanel_Alien_Image = toolkit.getImage("src/Images/loading.gif");
Should probably become
Image welcomePanel_Alien_Image = toolkit.getImage(getClass().getResource("/Images/loading.gif"));
Two things:
getImage(String)
assumes that the value is a "file" on the file system
- Once built, the resource will no longer be accessible from the
src
directory nor referenced as a "file"
As described here and here, the hotspot
parameter describes the offset from within the image at which a "click" point is generating. So you should most definitely not use new Point(welcomePanel.getX(), welcomePanel.getY())
because that's likely to cause no end of weird results.
Under Windows, I believe you stuck to a cursor size of 32x32, might have changed for Windows 10/Java 8+, but I've not tested it
For this example I used...
and
as my test cursors.
The ONLY way I was able to get the GIF to display was to use ImageIO.read
to load it, which meant that it would not animate. Attempting to load the image with ImageIcon
or Toolkit#createImage
failed to display anything, I even employed a MediaTracker
to try and ensure the image was loaded before creating the cursor
import java.awt.Cursor;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("test");
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton normal = new JButton("Normal");
JButton gif = new JButton("GIF");
JButton png = new JButton("PNG");
add(normal);
add(gif);
add(png);
normal.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setCursor(Cursor.getDefaultCursor());
}
});
gif.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// try {
// Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/cursor.gif"));
// MediaTracker mt = new MediaTracker(TestPane.this);
// mt.addImage(image, 1);
// mt.waitForAll();
// Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "gifCursor");
//
// setCursor(cursor);
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// }
try {
Image image = ImageIO.read(getClass().getResource("/images/cursor.gif"));
Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "gifCursor");
setCursor(cursor);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
png.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
Image image = ImageIO.read(getClass().getResource("/images/cursor.png"));
Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "pngCursor");
setCursor(cursor);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
}