Reputation: 41
I'm working on learning swing graphics, and I wanted to try to change the cursor color in java. So I downloaded a gif file of some golden stars to try to use for my cursor, but when I added the image as an ImageIcon, it turned the stars' colors to black.
EDIT: Changed grammar
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class stuff {
public static void main (String[] args)
{
JFrame frame = new JFrame("FRAME");
JPanel lpanel = new JPanel();
frame.setContentPane(lpanel);
ImageIcon goldStar = new ImageIcon("./res/goldStar.gif");
JLabel gs = new JLabel(goldStar);
lpanel.add(gs);
goldStar = new ImageIcon(goldStar.getImage().getScaledInstance((int)(goldStar.getIconWidth()/13), (int)(goldStar.getIconHeight()/13), Image.SCALE_SMOOTH));
lpanel.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(goldStar.getImage(),new Point(0,0), "custom cursor"));
//I think these two lines are creating the problem.
lpanel.setPreferredSize(new Dimension(1600,900));
frame.setVisible(true);
frame.pack();
}
}
I looked through some of the methods for cursor, but I didn't find anything that could help me out.
This is the picture I'm trying to integrate as my mouse cursor:
Upvotes: 3
Views: 2065
Reputation: 17534
You should consider using a non-animated image, since the documentation of Toolkit.createCustomCursor states that :
Note that multi-frame images are invalid and may cause this method to hang.
Alternatively, you may use an array of Cursor
objects (custom ones in your case), and create the animation in a Thread
.
Here is an example : Change Cursor in a thread for animation : Cursor.
Upvotes: 2
Reputation: 3035
You can load frames of gif image as described here and loop over them.
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
private int currentIndex;
public Main() throws IOException {
JFrame frame = new JFrame("FRAME");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.setContentPane(panel);
List<Cursor> cursors = new ArrayList<>();
List<BufferedImage> frames = getFrames("GWigb.gif");
for (BufferedImage image : frames) {
cursors.add(Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "cursor image"));
}
Timer timer = new Timer(50, (actionEvent) -> {
panel.setCursor(cursors.get(currentIndex++));
if (currentIndex >= cursors.size())
currentIndex = 0;
});
timer.start();
panel.setPreferredSize(new Dimension(1600, 900));
frame.setVisible(true);
frame.pack();
}
public List<BufferedImage> getFrames(String gif) throws IOException {
List<BufferedImage> frames = new ArrayList<>();
ImageReader reader = ImageIO.getImageReadersByFormatName("gif").next();
File input = new File(gif);
ImageInputStream stream = ImageIO.createImageInputStream(input);
reader.setInput(stream);
int count = reader.getNumImages(true);
for (int index = 0; index < count; index++) {
frames.add(reader.read(index));
}
return frames;
}
public static void main(String[] args) throws IOException {
new Main();
}
}
Upvotes: 3