3

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.stack.imgur.com/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);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dezrik
  • 169
  • 1
  • 8
  • 2
    A picture is worth a thousand words. – Jeffrey Feb 27 '12 at 02:53
  • 1
    If you don't have the rep. to upload the image here, upload it to an image sharing site and link. I suspect the 'dot' is in the image, but PS and Windows Photo Viewer show a similar colored BG that hides the dot. – Andrew Thompson Feb 27 '12 at 03:24
  • @Jeffrey Great comment. But also a great answer on [this question](http://stackoverflow.com/q/9456659/418556) (it was right, even if the OP did not realize it). Care to un-delete it so I can up-vote? – Andrew Thompson Feb 27 '12 at 04:21
  • @AndrewThompson, I've uploaded the image. I don't think the dot is there at all. But thank you for your comment. It's supposed to be a black dot. – Dezrik Feb 27 '12 at 06:42
  • 1
    Huhh... I wrote up some quick code to show you how to fix, only to see exactly the same confounding effect you do, and have all my expectations 'blown out of the water'. I edited the code I used into your question, since it was not in itself an answer, but might encourage others to investigate. Good question! – Andrew Thompson Feb 27 '12 at 07:17
  • 1
    Thank you for the help you provided @AndrewThompson. It truly is a confounding bug. It may be a problem with the icon maker application, or the way it was saved. I hope someone finds an answer. For the meantime, I think I should just redraw the image. Hopefully, it doesn't occur again. :) – Dezrik Feb 27 '12 at 07:24
  • @Dezrik *"For the meantime, I think I should just redraw the image."* That is an excellent idea. While it won't explain the behavior, it will most probably fix the problem (with a single icon or two) quicker than figuring *why* it is going wrong. And sometimes you need to abandon these small mysteries & instead 'go with what works'. :) – Andrew Thompson Feb 27 '12 at 07:29

1 Answers1

1

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);
Zecas
  • 647
  • 4
  • 23