tags:

views:

737

answers:

1

I'm trying to work out how to make SWT widgets (e.g. Label, Frame) be some shape other than rectangular.

I've made a custom shaped main window using the setRegion() method. Now I would like the widgets in the window to follow the same shape. I've tried using the setRegion() method on the widgets themselves (they inherit it) but nothing happens.

How do I make an SWT widget have a custom shape?

+3  A: 

I've managed to work it out. It seems my difficulty was arising from a misunderstanding of how to define a custom region. The setBounds() method defines the coordinate system for the custom region. I was assuming the region and setBounds were using the same coordinate system and the result was not displaying the widget at all.

The picture displays the effect. The green label on the left curves like the main window, while the grey frame on the bottom right corner doesn't.

The image appears in the preview but not the posted answer, try: http://img87.imageshack.us/my.php?image=curvecroppedwf8.png

The code fragments to do this:

Display display = Display.getDefault();
Shell shell new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);

// make the region for the main window
// circle is a method that returns a list of points defining a circle
Region region = new Region();
region.add(350, 0, 981, 51);
region.add(circle(380,51,30));
region.add(circle(951,51,30));
region.add(380, 51, 571, 30);
shell.setRegion(region);
Rectangle rsize = region.getBounds();
shell.setSize(rsize.width, rsize.height);

Composite main = new Composite(shell, SWT.NULL);

// make the label
cpyLbl = new Label(main, SWT.NONE);
cpyLbl.setText("Copy");

cpyLbl.setBackground(SWTResourceManager.getColor(38,255,23));

Region cpyRegion = new Region();
cpyRegion.add(0, 0, 161, 51);
cpyRegion.add(circle(28,51,28));
cpyRegion.add(28, 51, 133, 28);
cpyLbl.setRegion(cpyRegion);

// the top left of the bounds is the 0,0 of the region coordinates
// bounds are in screen coordinates (maybe shell coordinates?)
cpyLbl.setBounds(352, 0, 161, 79);
cpyLbl.setVisible(true);
cursa