繁体   English   中英

SWT外壳带圆角

[英]SWT Shell with round corners

我一直试图创建一个圆角的SWT Shell但没有成功。

谷歌在这个领域并没有多少帮助,我一直在尝试在shell上设置一个Region,但我找不到一种方法来创建一个圆形矩形区域。 如果有人能指出我正确的方向会很棒。

谢谢

检查这些链接

壳牌透明度
非矩形壳

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

最后不是那么难...... 4个圆圈和2个矩形:

swt圆形区域

这里我写的实用程序:

/**
 * 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)是(我把它从一些这个 SWT片段):

/**
 * 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM