带圆角的 SWT 外壳 [英] SWT Shell with round corners

查看:32
本文介绍了带圆角的 SWT 外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建带有圆角的 SWT Shell,但没有成功.

I have been trying to create a SWT Shell with rounded corners with no success.

Google 在这方面帮助不大,我一直在尝试在 shell 上设置区域,但找不到制作圆角矩形区域的方法.如果有人能指出我正确的方向,那就太棒了.

Google does not help much on this area, and I have been trying to set a Region on the shell, but I cannot find a way to make a region that is a round rectangle. If somebody could point me on the right direction would be awesome.

谢谢

推荐答案

我相信唯一的方法是手动构建你的圆形Region,然后将其设置到shell(或任何 SWT 控件).

I believe the only way is to manually build your rounded Region, then set it to the shell (or any SWT control anyway).

最后也没有那么难……4个圆2个矩形:

It is not that hard in the end... 4 circles and 2 rectangles:

这里是我写的实用程序:

Here the utility I wrote:

/**
 * Creates a region that delineates a rounded rectangle:
 * 
 * @param x     the initial X corner (of the original rectangle)
 * @param y     the initial Y corner (of the original rectangle)
 * @param W     the max width of the rectangle
 * @param H     the max height of the rectangle
 * @param r     the radius of the rounding circles
 * @return the following region:
 * <pre>
 *       P0 (x,y)
 *       . ___ _ _ _ _ _ _ _ _ _ _ _ ___
 *        /   \                     /   \    A
 *       |  ·  |                   |  ·  |   :
 *        \___/                     \___/    :
 *       |   <->                         |   :
 *            r                              :       
 *       |                               |   :
 *                                           :       
 *       |                               |   : H
 *                                           :
 *       |                               |   : 
 *                                           :       
 *       |                               |   :
 *                                           :
 *       | ___                       ___ |   :
 *        /   \                     /   \    :
 *       |  ·  |                   |  ·  |   :
 *        \___/ _ _ _ _ _ _ _ _ _ _ \___/    v
 *        
 *       <------------------------------->
 *                       W
 * </pre>
 */
public static Region createRoundedRectangle(int x, int y, int W, int H, int r) {
    Region region = new Region();
    int d = (2 * r); // diameter

    region.add(circle(r, (x + r), (y + r)));
    region.add(circle(r, (x + W - r), (y + r)));
    region.add(circle(r, (x + W - r), (y + H - r)));
    region.add(circle(r, (x + r), (y + H - r)));

    region.add((x + r), y, (W - d), H);
    region.add(x, (y + r), W, (H - d));

    return region;
}

circle(int, int, int) 在哪里(我从一些 this SWT 片段):

Where circle(int, int, int) is (I took it from some this SWT snippet):

/**
 * Defines the coordinates of a circle.
 * @param r         radius
 * @param offsetX   x offset of the centre
 * @param offsetY   y offset of the centre
 * @return the set of coordinates that approximates the circle.
 */
public static int[] circle(int r, int offsetX, int offsetY) {
    int[] polygon = new int[8 * r + 4];
    // x^2 + y^2 = r^2
    for (int i = 0; i < 2 * r + 1; i++) {
        int x = i - r;
        int y = (int) Math.sqrt(r * r - x * x);
        polygon[2 * i] = offsetX + x;
        polygon[2 * i + 1] = offsetY + y;
        polygon[8 * r - 2 * i - 2] = offsetX + x;
        polygon[8 * r - 2 * i - 1] = offsetY - y;
    }
    return polygon;
}

这篇关于带圆角的 SWT 外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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