0

Hello I have this piece of code to convert the cursor to a gif image that I have.

Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image welcomePanel_Alien_Image = toolkit.getImage("src/Images/loading.gif");
        Cursor welcomePanel_Alien_Cursor = toolkit.createCustomCursor
        (welcomePanel_Alien_Image , new Point(welcomePanel.getX(), 
         welcomePanel.getY()), "img");

         welcomePanel.setCursor (transparentCursor);

but the only thing is the program run without displaying anything at all ( Note the program runs perfectly with png image format ). So one of the solution I read on here it says that I need to replace this :

Image welcomePanel_Alien_Image = toolkit.getImage("src/Images/loading.gif");

with this :

 Image image = toolkit.getImage(getClass().getResource("pencil.gif"));

but when I do that I get this error

Exception in thread "main" java.lang.NullPointerException at sun.awt.SunToolkit.getImageFromHash(Unknown Source) at sun.awt.SunToolkit.getImage(Unknown Source)

Any Help would be appreciated

Samer34
  • 13
  • 6
  • `src/Images/loading.gif` should be something more like `/Images/loading.gif` assuming the image name `loading.gif` is in the `images` directory and they are stored correctly within the context of the application's class path (ie bundled within the Jar) - I'd also be interested in knowing if it's actually possible for the OS to display a GIF based cursor – MadProgrammer Mar 16 '17 at 23:21
  • I changed it to `/Images/loading.gif` and now I don't get an error but I got the same problem which I had fist which is that the program dies not display anything. Any ideas of why this is happening ? – Samer34 Mar 16 '17 at 23:24
  • The "hotspot" offset (`new Point(welcomePanel.getX(), welcomePanel.getY())`) is not the offset of the component, but the offset within the image at which the "click" action occurs - Take a look at [this example](http://stackoverflow.com/questions/19483732/mouse-jumping-when-i-enter-a-label/19483764#19483764) for more details – MadProgrammer Mar 16 '17 at 23:25
  • And [this example](http://stackoverflow.com/questions/23902159/java-custom-cursor-wont-work-on-new-computer/23902201#23902201) – MadProgrammer Mar 16 '17 at 23:27
  • @MadProgrammer But the thing is that the program does not run at all. Do you know how to fix it ? thanks – Samer34 Mar 16 '17 at 23:28
  • @MadProgrammer But how come it works for normal png image. I only have problem for gif files. When I use png the cursor is changed to the icon I have when I am at the welcome panel – Samer34 Mar 16 '17 at 23:29
  • Consider providing a runnable [example which demonstrates your problem](http://stackoverflow.com/help/mcve) - as I said, the OS my not be capable for display GIF image as a cursor – MadProgrammer Mar 16 '17 at 23:31
  • I am using Windows 10, The time it would take me to make an example that can be demonstrated will be the same as me solving this issue. I understand that you need the code to solve it, I was just wondering if some has faced the same problem before so that they could help immediately. Anyways, thanks for your time. – Samer34 Mar 16 '17 at 23:40

1 Answers1

1

Okay, so, this is probably a good indication that I need to go back to bed

Image welcomePanel_Alien_Image = toolkit.getImage("src/Images/loading.gif");

Should probably become

Image welcomePanel_Alien_Image = toolkit.getImage(getClass().getResource("/Images/loading.gif"));

Two things:

  1. getImage(String) assumes that the value is a "file" on the file system
  2. Once built, the resource will no longer be accessible from the src directory nor referenced as a "file"

As described here and here, the hotspot parameter describes the offset from within the image at which a "click" point is generating. So you should most definitely not use new Point(welcomePanel.getX(), welcomePanel.getY()) because that's likely to cause no end of weird results.

Under Windows, I believe you stuck to a cursor size of 32x32, might have changed for Windows 10/Java 8+, but I've not tested it

For this example I used...

Animated GIF and PNG as my test cursors.

The ONLY way I was able to get the GIF to display was to use ImageIO.read to load it, which meant that it would not animate. Attempting to load the image with ImageIcon or Toolkit#createImage failed to display anything, I even employed a MediaTracker to try and ensure the image was loaded before creating the cursor

import java.awt.Cursor;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("test");
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            JButton normal = new JButton("Normal");
            JButton gif = new JButton("GIF");
            JButton png = new JButton("PNG");

            add(normal);
            add(gif);
            add(png);

            normal.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setCursor(Cursor.getDefaultCursor());
                }
            });

            gif.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
//                    try {
//                        Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/cursor.gif"));
//                        MediaTracker mt = new MediaTracker(TestPane.this);
//                        mt.addImage(image, 1);
//                        mt.waitForAll();
//                        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "gifCursor");
//                        
//                        setCursor(cursor);
//                    } catch (InterruptedException ex) {
//                        ex.printStackTrace();
//                    }
                    try {
                        Image image = ImageIO.read(getClass().getResource("/images/cursor.gif"));
                        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "gifCursor");

                        setCursor(cursor);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
            png.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        Image image = ImageIO.read(getClass().getResource("/images/cursor.png"));
                        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0, 0), "pngCursor");

                        setCursor(cursor);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

    }

}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • It would have been perfect if the program animated the GIF. But I have learnt new things from ur answer. Thanks, I appreciate it :) Have a good night ;D – Samer34 Mar 17 '17 at 01:11