本文介绍了半透明光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我在摇摆中创建自定义半透明光标吗?我需要为这个光标设置一些图像,例如,如果我在面板上重叠一些文本,我需要在光标下看到这个文本。

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.

推荐答案

对光标使用半透明图像。 AFAIU是J2SE理解的唯一支持部分透明的图像类型 - 是PNG。

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

Metal和默认的Windows PLAF似乎都没有以我理解的任何方式支持部分透明。

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.

这篇关于半透明光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 01:55