java swt shell_java – 带圆角的SWT Shell

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

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

6387e3c3c4421f36a3340828e76cbb86.png

这里我写的实用程序:

/**

* 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:

*

 
 

* P0 (x,y)

* . ___ _ _ _ _ _ _ _ _ _ _ _ ___

* / \ / \ A

* | · | | · | :

* \___/ \___/ :

* | | :

* r :

* | | :

* :

* | | : H

* :

* | | :

* :

* | | :

* :

* | ___ ___ | :

* / \ / \ :

* | · | | · | :

* \___/ _ _ _ _ _ _ _ _ _ _ \___/ v

*

*

* W

*

*/

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片段中获取):

/**

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值