1

Possible Duplicate:
Need to create cursor with watermark image

Can somebody help me to create custom semi-transparent cursor in swing? I need to set some image to this cursor and for instance if I'm overlaping some text on panel I need to see this text under my cursor.

Community
  • 1
  • 1

2 Answers2

5

Use a semi-transparent image for the cursor. AFAIU the only image type understood by J2SE that supports partial transparency - is PNG.


Neither Metal nor the default Windows PLAF seems to support partial transparency in any way I understand it.

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 upshot is - I was wrong. Using a semi-transparent icon will not achieve the goal.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • it would be nice to see some piec of code – Andriy Kushka Dec 28 '11 at 09:30
  • 4
    Just as it would be nice to see capital letters at the start of sentences, your best code attempt as an [SSCCE](http://sscce.org/), a link to a semi-transparent 16x16 icon (or at least a 16x16 icon).. – Andrew Thompson Dec 28 '11 at 09:33
  • interesting ... would have bet to having seen java created transparent cursors - and would have lost :-) Thanks for the sscce! – kleopatra Dec 28 '11 at 12:29
  • @kleopatra Yes, the 'failure' result surprised me as well. I was considering deleting the original answer, but since I had the source.. ;) – Andrew Thompson Dec 28 '11 at 12:39
  • @Andrew Thompson I can't found that right now, but solved by using translucent JWindow that follows mouse on the Screen, but maybe I bet too that works in earlier Java6 versions – mKorbel Dec 28 '11 at 12:50
1

This might 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;
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Gokul
  • 455
  • 6
  • 17
  • Thank you for answer but this not exactly what I mean, can I do my image like glass? – Andriy Kushka Dec 28 '11 at 07:51
  • i guess, its better to post a portion of your code here. – Gokul Dec 28 '11 at 08:18
  • I do not have some special code, I just have a panel, on panel I have a button with some text and I need when my custom cursor move above the button I need to see the picture on the cursor and text under this picture(it should be like glass or watermark). Can I do that using the swing? – Andriy Kushka Dec 28 '11 at 08:35