title: Swing: How to set Custom Cursor as transparent?
body: <p>I'm changing a cursor, using a PNG image (with transparency), but when I run the code below, the image doesn't look like it should.</p>
  <pre><code>public void CustomCursor()
  {
      Toolkit t1 = Toolkit.getDefaultToolkit();
      Image img = t1.getImage(&quot;src/AppImages/Cursor1.png&quot;);
      Point point = new Point(0,0);
      Cursor cursor = t1.createCustomCursor(img, point, &quot;Cursor&quot;);
      setCursor(cursor);    
  }
  </code></pre>
  <p>This method is called in the Jframe's constructor.</p>
  <p><a href="https://i.stack.imgur.com/sJKuE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sJKuE.png" alt="enter image description here" /></a></p>
  <p>This is the <code>cursor1.png</code> image, sized 25x25px.</p>
  <p>After running the code:</p>
  <p><a href="https://i.stack.imgur.com/cMxUG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cMxUG.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/ggMJE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ggMJE.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/0JON1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0JON1.png" alt="enter image description here" /></a></p>
  <p>If I use <code>cursor1.png</code> as a <code>JLabel</code>, it looks OK:</p>
  <p><a href="https://i.stack.imgur.com/UB2T9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UB2T9.png" alt="enter image description here" /></a></p>
  <h2>MCVE</h2>
  <pre><code>import java.awt.*;
  import java.net.*;
  import java.util.logging.*;
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  
  public class CustomCursor {
  
      private JComponent ui = null;
  
      CustomCursor() {
          initUI();
      }
  
      public void initUI() {
          if (ui != null) {
              return;
          }
  
          ui = new JPanel(new BorderLayout(4, 4));
          ui.setBorder(new EmptyBorder(20, 200, 20, 200));
          Toolkit t1 = Toolkit.getDefaultToolkit();
          Image img;
          try {
              URL url = new URL(&quot;http://i.stack.imgur.com/sJKuE.png&quot;);
              img = t1.getImage(url);
              Point point = new Point(0, 0);
              Cursor cursor = t1.createCustomCursor(img, point, &quot;Cursor&quot;);
              ui.setCursor(cursor);
              ui.add(new JLabel(new ImageIcon(url)));
          } catch (MalformedURLException ex) {
              Logger.getLogger(CustomCursor.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      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) {
                  }
                  CustomCursor o = new CustomCursor();
  
                  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);
      }
  }
  </code></pre>
  <p>Can anyone tell why that happens?</p>