I want to move a JFrame window containing a GIF across my screen, but when I try the following:
icon = new ImageIcon(spritePath);
frame.add(new JLabel(icon));
frame.setSize(icon.getIconWidth(), icon.getIconHeight());
frame.setVisible(true);
for (int i = 0; i < screenSize.getWidth() - icon.getIconWidth(); i++) {
frame.setLocation(i, yMidpoint);
Thread.sleep(10);
}
My window moves but the gif doesn't show up. I also tried increasing the sleep delay to a higher number like 1000 and that didn't work. And the gif shows when I don't move it with setLocation(), so I'm not sure what the problem is.
I figured out that the issue was that Thread.sleep()
prevents the window's contents from being painted if it is run in the same thread that the JFrame is created, so I simply had it run the code in a new thread and now it works.