RoundRectangle2D剪辑不是很平滑 [英] RoundRectangle2D clip is not very smooth

查看:403
本文介绍了RoundRectangle2D剪辑不是很平滑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPanel,我想剪一下角,使其边缘变圆.

I have a JPanel that i want to clip the corners so that it has rounded edge.

这就是我在做什么.

((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(color);
Shape s = new RoundRectangle2D.Double(0, 0, width, height, arc, arc);
g.setClip(s);

因此请注意,我正在将clipping设置为RoundRectangle2D.我也设置anti-aliasing,但我的圆角边缘确实是锯齿状的.

So notice that i am setting the clipping to a RoundRectangle2D. Also i am setting anti-aliasing still my rounded edges are really jagged.

软剪辑示例,此链接包含一个对图像进行柔软圆角处理的方法.如何将其应用于JPanel?

Soft clipping example this link has a way to do soft rounded edges for a image. How do i apply the same to a JPanel?

推荐答案

因为剪裁没有混叠,所以您会看到锯齿状的边缘.尝试边界:

because clipping is not aliased, you see the jagged edges. try a border instead:

 p.setBorder(new RoundedBorder(p.getBackground(), 2, 16));

其中RoundedBorder改编自文本气泡类:

where RoundedBorder is adapted from the text bubble class:

 class RoundedBorder extends AbstractBorder {

    private Color color;
    private int thickness = 4;
    private int radii = 8;
    private Insets insets = null;
    private BasicStroke stroke = null;
    private int strokePad;
    private int pointerPad = 4;
    RenderingHints hints;

    RoundedBorder(
            Color color, int thickness, int radii) {
        this.thickness = thickness;
        this.radii = radii;

        this.color = color;

        stroke = new BasicStroke(thickness);
        strokePad = thickness / 2;

        hints = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        int pad = radii + strokePad;
        int bottomPad = pad + strokePad;
        insets = new Insets(pad, pad, bottomPad, pad);
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return insets;
    }

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

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

        Graphics2D g2 = (Graphics2D) g;

        int bottomLineY = height - thickness;

        RoundRectangle2D.Double bubble = new RoundRectangle2D.Double(
                0 + strokePad,
                0 + strokePad,
                width - thickness,
                bottomLineY,
                radii,
                radii);

        Area area = new Area(bubble);

        g2.setRenderingHints(hints);

        Area spareSpace = new Area(new Rectangle(0, 0, width, height));
        spareSpace.subtract(area);
        g2.setClip(spareSpace);
        g2.clearRect(0, 0, width, height);
        g2.setClip(null);

        g2.setColor(color);
        g2.setStroke(stroke);
        g2.draw(area);
    }
}
}

这篇关于RoundRectangle2D剪辑不是很平滑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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