gpt4 book ai didi

java - 带圆角的 SWT 外壳

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:46:57 24 4
gpt4 key购买 nike

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

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

谢谢

最佳答案

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

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

swt rounded region

这里是我写的实用程序:

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

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

关于java - 带圆角的 SWT 外壳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431269/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com