Search code examples
javaimageswingmouse-cursor

Why when I set an image to the cursor, it doesn't work?


I'm new in Java and I have a problem trying set an image to the cursor. I'm using a BufferedImage and Graphics.drawImage but it is only drawing a color of the image and not the full png image.

Here is my code:

/*The images List*/
iconsBet.add(ImageIO.read(getClass().getResource("/resources/ChipType"+ String.valueOf(maxChipBet+1) +".png")));
/*The images List*/

BufferedImage output = new BufferedImage(iconsBet.get(0).getWidth(), iconsBet.get(0).getHeight(), BufferedImage.TYPE_INT_ARGB );
Graphics graphicsCursorIcon = output.getGraphics();

int count = 0;
for(BufferedImage icon : iconsBet)
{                
   graphicsCursorIcon.drawImage(icon, 0, count*10, null);
   count++;
}

graphicsCursorIcon.dispose();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor c = toolkit.createCustomCursor(output , new Point(mainPanel.getX(), mainPanel.getY()), "img");
mainPanel.setCursor(c);

The image: This is one image from the group of images that I'm using

The program only draw a red circle and not the png image.

I already tried use all the BufferedImage types, but yet doesn't work. Could you please help me with this? What do I need to do to make it work?


Solution

  • This MCVE works here, though it shrinks the cursor down to a smaller size.

    import java.awt.*;
    import java.awt.image.*;
    import java.io.IOException;
    import javax.imageio.*;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import java.net.URL;
    
    public class CustomImageCursor {
    
        private JComponent ui = null;
    
        CustomImageCursor() {
            initUI();
        }
    
        public void initUI() {
            if (ui != null) {
                return;
            }
    
            ui = new JPanel(new BorderLayout(4, 4));
            ui.setBorder(new EmptyBorder(40, 400, 40, 40));
    
            try {
                BufferedImage bi = ImageIO.read(
                        new URL("https://i.sstatic.net/b89MA.png"));
                Toolkit toolkit = Toolkit.getDefaultToolkit();
                Cursor c = toolkit.createCustomCursor(bi, new Point(0, 0), "img");
                ui.setCursor(c);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public JComponent getUI() {
            return ui;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception useDefault) {
                    }
                    CustomImageCursor o = new CustomImageCursor();
    
                    JFrame f = new JFrame(o.getClass().getSimpleName());
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    f.setLocationByPlatform(true);
    
                    f.setContentPane(o.getUI());
                    f.pack();
                    f.setMinimumSize(f.getSize());
    
                    f.setVisible(true);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }