omm118
omm118

Reputation: 315

Java SWT - Transparent background on canvas?

Is it possible to fill a rectangle and an oval over a drawn image and having the oval being a "window" to the image behind it?

e.gc.drawImage(//...);
e.gc.setBackground(//black);
e.gc.fillRectangle(//over the image);
e.gc.setBackground(//SWT.TRANSPARENT?);
e.gc.fillOval(somewhere on the canvas);

this snippet obviously isn't working for me, but can this even be achieved with SWT?

Upvotes: 2

Views: 773

Answers (2)

omm118
omm118

Reputation: 315

I managed to do it,

what I did was create a new shell with the SWT.NO_TRIM property (it's parent is the canvas/costume widget) and use setRegion() on it.

The trick was the region itself, it has a subtract() method, so I found a method that produces a polygon and I subtracted it from the region.

Than I set it and voilà. I had to add resize and move listeners so it would look "natural" (i.e, part of the canvas and not a new shell).

It works really well actually.

Upvotes: 1

Baz
Baz

Reputation: 36884

Instead of using fillOval() and fillRectangle(), use drawOval() and drawRectangle() to draw the outline instead of filling the area.

Here's the JavaDoc:

Draws the outline of an oval, using the foreground color, within the specified rectangular area.

Whereas fillOval() does:

Fills the interior of an oval, within the specified rectangular area, with the receiver's background color.

Upvotes: 0

Related Questions