1

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • On windows, you are restrict to a cursor size of 32x32 - it might have changed, but that was the case under Windows 7/Java 7 – MadProgrammer Apr 14 '17 at 02:33
  • You are drawing your images on top of each other. Are you sure they have proper transparency? May be the last image completely overpaints all the previous ones. – Thomas Fritsch Apr 14 '17 at 02:52
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). Though in this case, you might hot-link to the image embedded in the question. – Andrew Thompson Apr 14 '17 at 10:00

2 Answers2

5

I suspect you misinterpreted the second argument of Toolkit.createCustomCursor(Image cursor, Point hotSpot, String name):

hotSpot - the X and Y of the large cursor's hot spot; the hotSpot values must be less than the Dimension returned by getBestCursorSize

The hotspot is meant relative to the top-left corner of the cursor image, not to the top-left corner of the panel. So, instead of

new Point(mainPanel.getX(), mainPanel.getY())

just try

new Point(0, 0)
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
  • Thank you very much, you're right, i misinterpreted that. Already i corrected it, but it isn't the reason of my problem. Because i need only copy the button image and draw it on the cursor, but it only draw a circle with a color and not the full PNG image. Thank you very much for you observation. – Darth Hollow Apr 14 '17 at 02:13
  • @DarthHollow Then there is still a problem with your images. You should add these to your question to get good answers. – Thomas Fritsch Apr 14 '17 at 02:20
  • Thank you for the advice, already i put the image in the question. – Darth Hollow Apr 14 '17 at 02:28
  • @DarthHollow Your image is quite large (300x298). I don't know if that would cause problems. But typical cursor images are not larger than 32x32. – Thomas Fritsch Apr 14 '17 at 02:36
2

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.stack.imgur.com/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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433