Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Cursor Icon is loaded with an extra shaded pixel

I changed the cursor Icon whenever a certain toggle button is clicked. But the loaded image contains an extra pixel in it at the bottom corner! It's annoying, like there's constant dirt on the screen. I created the cursor icon using junior icon editor. When I open the picture using windows photo viewer or photoshop, the pixel doesn't manifest itself. It only shows when I use it in the application.

The application is a Java application, and this is how I set the cursor.

Image img = getResourceMap().getImageIcon( iconFilename ).getImage();
        Cursor newCursor = Toolkit.getDefaultToolkit().createCustomCursor( img,
            new Point( 5, 5 ), "cursor" );

Does anyone know what could be the possible reasons for this extra shaded pixel? It occurs right below the east-facing arrow, about 2 milimeters below it.

enter image description here

You can see the effect running this code. The image appears as expected in a label, but as a pointer there is a darkened pixel on the lower left.

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

import javax.imageio.ImageIO;
import java.net.URL;

class ShowImage {

    public static void main(String[] args) throws Exception {
        URL url = new URL("https://i.sstatic.net/kP1jv.png");
        final BufferedImage img = ImageIO.read(url);
        System.out.println(
            "Image is: " + img.getWidth() + "x" + img.getHeight());
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                JLabel l = new JLabel(new ImageIcon(img));
                l.setBorder(new EmptyBorder(5,5,5,5));
                l.setOpaque(true);
                l.setBackground(Color.GREEN.darker());
                Cursor newCursor = Toolkit.getDefaultToolkit().
                    createCustomCursor( img,new Point( 5, 5 ), "c" );
                l.setCursor(newCursor);
                JOptionPane.showMessageDialog(null, l);
            }
        });
    }
}
like image 350
Dezrik Avatar asked Jan 26 '26 13:01

Dezrik


1 Answers

At first i thought that we were specifying a bigger image than the system cursor size could handle but than i realized that if we remove the last column(!!) of pixels it worked fine!! I don't know how to explain this!

BufferedImage originalImg = ImageIO.read(new File("kP1jv.png"));
System.out.println("originalImage is: " + originalImg.getWidth() + "x"
                  + originalImg.getHeight());
Dimension d = Toolkit.getDefaultToolkit().getBestCursorSize(
                originalImg.getWidth(), originalImg.getHeight());

final BufferedImage img = originalImg.getSubimage(0, 0, d.width/*-1*/, d.height-1);
like image 169
Zecas Avatar answered Jan 28 '26 02:01

Zecas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!