半透明的JFrame边框 [英] Translucent JFrame border

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

问题描述

我想让JFrame边框透明,所以我尝试使用我自己的Border类...

I want make JFrame border transparent, so I am trying to do so using my own Border class...

private class ShadowBorder extends AbstractBorder {

    private static final int RADIUS = 30;

    @Override
    public boolean isBorderOpaque() {
        return false;
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(RADIUS, RADIUS, RADIUS, RADIUS);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets) {
        insets.top = RADIUS;
        insets.left = RADIUS;
        insets.bottom = RADIUS;
        insets.right = RADIUS;
        return insets;
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                 RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(new Color(0x66000000, true));

            g2d.fillRect(0, 0, width - RADIUS, RADIUS);
        }
}

但最终边框不透明。边框内还有白色背景,但我不知道为什么(见atach。图片)。有什么想法?

but border is not transparent in final. There is also white background inside of border, but I do not know why (see atach. image). Any ideas?

谢谢!

推荐答案

您需要将窗口设置为不透明和在Graphics上使用复合。此外,在您的代码中,您只打印一个边框,而不是四个边框,这就是为什么您只看到一个边框被绘制的原因。这样的事情应该这样做(虽然最好根据插图绘制边框,而不是你的RADIUS常量):

You need to set the window as not opaque and use a composite on the Graphics. Also, in your code, you are only printing one border, not the four of them, this is why you see only one border painted. Something like this should do it (although it would be better to paint the borders based on the insets, not your RADIUS constant):

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.io.UnsupportedEncodingException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.AbstractBorder;

import com.sun.awt.AWTUtilities;

public class Main {

    private static class ShadowBorder extends AbstractBorder {

        private static final int RADIUS = 30;

        @Override
        public boolean isBorderOpaque() {
            return false;
        }

        @Override
        public Insets getBorderInsets(Component c) {
            return new Insets(RADIUS, RADIUS, RADIUS, RADIUS);
        }

        @Override
        public Insets getBorderInsets(Component c, Insets insets) {
            insets.top = RADIUS;
            insets.left = RADIUS;
            insets.bottom = RADIUS;
            insets.right = RADIUS;
            return insets;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(new Color(66, 0, 0));
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 0.5f));
            g2d.fillRect(0, 0, width - RADIUS, RADIUS);
            g2d.fillRect(width - RADIUS, 0, RADIUS, height - RADIUS);
            g2d.fillRect(0, RADIUS, RADIUS, height - RADIUS);
            g2d.fillRect(RADIUS, height - RADIUS, width - RADIUS, RADIUS);
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        AWTUtilities.setWindowOpaque(frame, false);
        JPanel panel = new JPanel(new BorderLayout());
        JButton button = new JButton("Hello");
        panel.add(button);
        panel.setBorder(new ShadowBorder());
        frame.setContentPane(panel);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆