你如何改变java中的光标颜色?

我正在研究摆动图形,我想尝试在java中更改光标颜色.所以我下载了一些金色星星的gif文件试图用于我的光标,但是当我将图像添加为ImageIcon时,它将星星的颜色变为黑色.

编辑:改变语法

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();
    }
}

我查看了一些光标方法,但是我找不到任何可以帮助我的方法.

这是我试图整合为我的鼠标光标的图片:
你如何改变java中的光标颜色?

解决方法:

您可以按照here所述加载gif图像的帧并循环它们.

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();
    }

}
上一篇:如何在Java中进行2D阴影投射?


下一篇:sharding-jdbc执行原理