如何设置JTextField的透明文本颜色

时间:2015-02-23 03:06:10

标签: java

我想设置JTextField的透明文本颜色,意味着JTextField上的文本颜色应该与添加了JTextField的JFrame颜色相同。请告诉我该怎么做?

1 个答案:

答案 0 :(得分:1)

这可能是一个巨大的矫枉过正,但正如我理解这个问题,基本上,这样做会使文本变得透明"或出现"切出"文本字段......

enter image description here

public class CutoutTextField extends JTextField {

    public CutoutTextField() {
        init();
    }

    public CutoutTextField(String text) {
        super(text);
        init();
    }

    public CutoutTextField(int columns) {
        super(columns);
        init();
    }

    public CutoutTextField(String text, int columns) {
        super(text, columns);
        init();
    }

    public CutoutTextField(Document doc, String text, int columns) {
        super(doc, text, columns);
        init();
    }

    protected void init() {
        setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        TextUI ui = getUI();

        // This is JUST the text
        BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D ig = img.createGraphics();
        applyQualityRenderingHints(ig);
        ui.paint(ig, this);
        ig.dispose();

        // This is the background of the field...
        BufferedImage bg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        ig = bg.createGraphics();
        applyQualityRenderingHints(ig);
        ig.setColor(getBackground());
        ig.fillRect(0, 0, getWidth(), getHeight());
        ig.dispose();

        BufferedImage masked = ImageUtilities.applyMask(img, bg, AlphaComposite.XOR);
        int y = (getHeight() - masked.getHeight()) / 2;
        g.drawImage(masked, 0, y, this);
    }

    public BufferedImage applyMask(BufferedImage sourceImage, BufferedImage maskImage, int method) {

        BufferedImage maskedImage = null;
        if (sourceImage != null) {

            int width = maskImage.getWidth(null);
            int height = maskImage.getHeight(null);

            maskedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D mg = maskedImage.createGraphics();
            applyQualityRenderingHints(mg);

            int x = (width - sourceImage.getWidth(null)) / 2;
            int y = (height - sourceImage.getHeight(null)) / 2;

            mg.drawImage(sourceImage, x, y, null);
            mg.setComposite(AlphaComposite.getInstance(method));

            mg.drawImage(maskImage, 0, 0, null);

            mg.dispose();

        }

        return maskedImage;

    }

    public void applyQualityRenderingHints(Graphics2D g2d) {

        g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

    }

}

明白,这是一次大规模的破解!

这基本上做的是将文字从字段绘制到BufferedImage,然后生成一个单独的BufferedImage填充字段的背景颜色,然后XOR&#39 ;将两个图像放在一起,有效地将文本切割出背景。然后它简单地绘制结果BufferedImage

有些人会注意到我没有打电话给super.paintComponent,这是故意做的,因为我不希望该字段绘制背景和文字,但希望控制该过程。< / p>

这是可能的,因为该字段是透明的,我可以调用super.paintComponent,但我会以任何方式绘制到其中一个BufferedImage ... <\ n / p>