Translucent cursor

Possible duplicate:
You need to create a cursor with a watermark

Can someone help me create a custom translucent cursor in swing? I need to set some image on this cursor and, for example, if I overlap some text on the panel, I need to see this text under my cursor.

0
source share
2 answers

Use a translucent image for the cursor. AFAIU the only image type that J2SE understands that supports partial transparency is PNG.


Neither Metal nor Windows PLAF support partial transparency by default, I understand that.

import java.awt.*; import java.awt.image.BufferedImage; import javax.swing.*; import javax.imageio.ImageIO; import java.io.File; import java.net.URL; /** The example demonstrates how a semi-transparent image is NOT supported as a cursor image. It is drawn as a solid color. */ class SemiTransparentCursor { public static void main(String[] args) { final BufferedImage biPartial = new BufferedImage( 32, 32, BufferedImage.TYPE_INT_ARGB); Graphics2D g = biPartial.createGraphics(); g.setColor(new Color(255,0,0,63)); int[] x = {0,32,0}; int[] y = {0,0,32}; g.fillPolygon(x,y,3); g.dispose(); final Cursor watermarkCursor = Toolkit.getDefaultToolkit(). createCustomCursor( biPartial, new Point(0, 0), "watermarkCursor"); SwingUtilities.invokeLater(new Runnable() { public void run() { JOptionPane.showMessageDialog( null, new ImageIcon(biPartial)); JEditorPane jep = new JEditorPane(); jep.setPreferredSize(new Dimension(400,400)); jep.setCursor(watermarkCursor); try { URL source = new File("SemiTransparentCursor.java"). toURI().toURL(); jep.setPage(source); } catch(Exception e) { e.printStackTrace(); } JOptionPane.showMessageDialog( null, jep); } }); } } 

The result - I was wrong. Using a translucent icon will not achieve the goal.

+4
source

This may solve your problem.

 public Cursor pointer() throws Exception { int[] pixels = new int[16 * 16]; Image image = Toolkit.getDefaultToolkit().createImage( new MemoryImageSource(16, 16, pixels, 0, 16)); Cursor transparentCursor = Toolkit.getDefaultToolkit().createCustomCursor( image, new Point(0, 0), "transparentCursor"); return transparentCursor; } 
+1
source

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


All Articles