Java cursor implementation with some transparency

I have 35x40 px. png image I want to use as a custom cursor in a Swing application. The image has a glow, so it contains alpha transparency values. The problem is that when I try to use the usual method of using Toolkit to create a custom cursor, I get black pixels where the alpha values ​​should be.

Here is the image I use for the cursor: https://dl.dropbox.com/u/1186703/cursor.png

Here is my code:

 public static void main(String[] args) throws IOException { new Sandbox().gui(); } private Cursor cursor; private Toolkit kit; private Image cursorImage; public void gui() { kit = Toolkit.getDefaultToolkit(); cursorImage = kit.createImage(getClass().getResource( "/aurora/V1/resources/cursor.png")); cursor = Toolkit.getDefaultToolkit().createCustomCursor( cursorImage, new Point(0, 0), "CustomCursor"); setSize(800, 800); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); setCursor(cursor); } 

Here is the current result:

result

To change it seems that this method does not work very well, for example, Windows LAF does not support translucency. Therefore, I am looking for any solution to make it work on Windows, assuming that this implementation really works on Mac OSX, I can simply indicate in the code which implementation to use based on the operating system in which the application is running.

+4
source share
4 answers

The problem is that you are dealing with the Cursor class, which (under Windows) does not take into account the transparency values ​​of the image

This is by no means a "real" solution, but more about a "fudging" result ...

 public class TestMouseCursor { public static void main(String[] args) { new TestMouseCursor(); } public TestMouseCursor() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException ex) { } catch (InstantiationException ex) { } catch (IllegalAccessException ex) { } catch (UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new MouseCursorPane()); frame.setSize(400, 400); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class MouseCursorPane extends JPanel { private BufferedImage cursorImage; private Toolkit kit; public MouseCursorPane() { try { kit = Toolkit.getDefaultToolkit(); cursorImage = ImageIO.read(getClass().getResource("/cursor02.png")); for (int i = 0; i < cursorImage.getHeight(); i++) { int[] rgb = cursorImage.getRGB(0, i, cursorImage.getWidth(), 1, null, 0, cursorImage.getWidth() * 4); for (int j = 0; j < rgb.length; j++) { int alpha = (rgb[j] >> 24) & 255; if (alpha < 128) { alpha = 0; } else { alpha = 255; } rgb[j] &= 0x00ffffff; rgb[j] = (alpha << 24) | rgb[j]; } cursorImage.setRGB(0, i, cursorImage.getWidth(), 1, rgb, 0, cursorImage.getWidth() * 4); } Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor( cursorImage, new Point(0, 0), "CustomCursor"); setCursor(cursor); } catch (Exception exp) { exp.printStackTrace(); } } } } 

I got an idea here

+4
source

Your code and cursor image actually gives the desired result on MacOS X 10.7.5 (jdk 1.6.0_31) with a translucent blue frame. But I noticed a remark in this answer , which says that partial transparency is not supported in the standard Windows view.

+6
source

If you are desperate and absolutely must have a transparent cursor, regardless of its consequences, you can use JNI and manually set the cursor using the Win32 API. Windows with XP supports alpha cursors, so you should be fine with this.

But you are losing platform independence. And based on Windows settings, alpha blending can be turned off for that particular user.

+2
source

An alternative is a fake cursor.

Take a look at Alexander Potochkin's Well Behaved GlassPane .

In particular, run the sample code, select Options>GlassPane is Visible and Options>Final GlassPane .

Starting with this, load the cursor image, which is completely transparent, then draw the correct alpha-shifted cursor on the glass glass.

+2
source

Source: https://habr.com/ru/post/1445256/


All Articles