I noticed a strange problem that sometimes showed up in my game where the custom cursor would be disabled if the alt key is pressed.
I tracked the problem down to the custom cursor will be disabled when the alt key is pressed if and only if the mouse isn’t moved and no other key is pressed in combination with the alt key.
Thinking this must be a bug in my game, I created a simple test program and was surprised to find the same problem.
I have searched google and JGO and not seen any mention of this problem.
I’m hoping someone knows a workaround or can point me to how I should be doing it different.
Here is the test program.
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class CustomCursorTest extends JFrame {
BufferedImage image;
Cursor cursor;
public CustomCursorTest() {
setPreferredSize(new Dimension(800, 600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String url = "http://www.java-gaming.org/Themes/SMF_Revamped_v2c/images/on.gif";
try {
image = ImageIO.read( new URL(url) );
cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(), "Cursor");
setCursor(cursor);
} catch (Exception e) {
e.printStackTrace();
}
pack();
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image, 100, 100, null);
}
/**
* @param args
*/
public static void main(String[] args) {
CustomCursorTest test = new CustomCursorTest();
test.setVisible(true);
}
}
- edited for clarity.