Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT Shell with round corners

Tags:

java

swt

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

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.

Thanks

like image 628
Hector Romero Avatar asked Mar 20 '23 05:03

Hector Romero


1 Answers

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

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

swt rounded region

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

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;
}
like image 141
Campa Avatar answered Mar 29 '23 02:03

Campa