如何为带有圆角的JDialog设置3D边框? [英] How to set a 3D border for a JDialog with rounded corners?

查看:268
本文介绍了如何为带有圆角的JDialog设置3D边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在JDialog中添加圆角边框,如

I could add a rounded corner border to my JDialog as in How to create a rounded title border in Java Swing. But it is still one color. I want to make the border looks like 3D.

这是我尝试的方式.

Graphics2D g2d = (Graphics2D) g;
        Color c1 = getBackground();
        Color c2 = color1.darker();
        int w = getWidth();
        int h = getHeight();

      GradientPaint gp = new GradientPaint(
                0, 0, c1,
                0, h, c2);

        g2d.setPaint(gp);
        g2d.fill3DRect(0,0, w, h,true);

然后,没有3D外观,但是边框的边框颜色进一步加宽.
我怎样才能做到这一点?

Then, no 3D look, but the border has been widen more with its border color.
How can I achieve this?

任何示例代码或链接都将受到高度赞赏.

Any sample code or links will be highly appreciated.

推荐答案

足够了吗?

这还远未达到完美,但基本思路可行...

It's far from perfect, but the basic idea works...

public class MyRoundedBorder implements Border {

    protected static final Insets DEFAULT_INSETS = new Insets(4, 4, 4, 4);

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {

        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2d.setColor(Color.WHITE);
        Shape corner = new RoundedShape(width - 8, height - 8);
        g2d.translate(x + 2, y + 2);
        g2d.draw(corner);
        g2d.transform(AffineTransform.getRotateInstance(Math.toRadians(180), (width - 8) / 2, (height - 8) / 2));
        g2d.setColor(Color.LIGHT_GRAY);
        g2d.draw(corner);

        g2d.dispose();

    }

    @Override
    public Insets getBorderInsets(Component c) {

        return DEFAULT_INSETS;

    }

    @Override
    public boolean isBorderOpaque() {

        return true;

    }

    public class RoundedShape extends Path2D.Float {

        public RoundedShape(int width, int height) {

            moveTo(0, height - 20);
            append(new Arc2D.Float(0, height - 20, 20, 20, 180, 45, Arc2D.CHORD), false);
            lineTo(0, 20);
            curveTo(0, 0, 0, 0, 20, 0);
            lineTo(width - 10, 0);
            append(new Arc2D.Float(width - 20, 0, 20, 20, 90, -45, Arc2D.CHORD), false);

        }

    }

}

这篇关于如何为带有圆角的JDialog设置3D边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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