private void disposeRegion(Control control) { Region region = control.getRegion(); if (region != null) { control.setRegion(null); region.dispose(); } }
void paint(PaintEvent e) { GC gc = e.gc; Point size = comp.getSize(); if (curveColor == null) curveColor = e.display.getSystemColor(SWT.COLOR_BLACK); int h = size.y; int[] simpleCurve = new int[] {0, h - 1, 1, h - 1, 2, h - 2, 2, 1, 3, 0}; // draw border gc.setForeground(curveColor); gc.setAdvanced(true); if (gc.getAdvanced()) { gc.setAntialias(SWT.ON); } gc.drawPolyline(simpleCurve); Rectangle bounds = ((Control) e.widget).getBounds(); bounds.x = bounds.y = 0; Region r = new Region(); r.add(bounds); int[] simpleCurveClose = new int[simpleCurve.length + 4]; System.arraycopy(simpleCurve, 0, simpleCurveClose, 0, simpleCurve.length); int index = simpleCurve.length; simpleCurveClose[index++] = bounds.width; simpleCurveClose[index++] = 0; simpleCurveClose[index++] = bounds.width; simpleCurveClose[index++] = bounds.height; r.subtract(simpleCurveClose); Region clipping = new Region(); gc.getClipping(clipping); r.intersect(clipping); gc.setClipping(r); Image b = toolParent.getBackgroundImage(); if (b != null && !b.isDisposed()) gc.drawImage(b, 0, 0); r.dispose(); clipping.dispose(); // // gc.fillRectangle(bounds); // Rectangle mappedBounds = e.display.map(comp, comp.getParent(), // bounds); // ((Composite) toolParent).drawBackground(gc, bounds.x, bounds.y, // bounds.width, // bounds.height, mappedBounds.x, mappedBounds.y); }
public void paint(GC gc, int x, int y) { // Remember clipping region Region region = new Region(); gc.getClipping(region); // Set clipping region so only the portion of the target we want is // printed. gc.setClipping(x, y, size.x, size.y); // Paint the target. target.paint(gc, x - offset.x, y - offset.y); // Restore clipping region gc.setClipping(region); region.dispose(); }
public static void main(String[] args) { final Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); final Shell shell = new Shell(display, SWT.NO_TRIM); Region region = new Region(); final ImageData imageData = image.getImageData(); if (imageData.alphaData != null) { Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < imageData.height; y++) { for (int x = 0; x < imageData.width; x++) { if (imageData.getAlpha(x, y) == 255) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } else { ImageData mask = imageData.getTransparencyMask(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if (mask.getPixel(x, y) != 0) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } shell.setRegion(region); Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ int startX, startY; @Override public void handleEvent(Event e) { if (e.type == SWT.KeyDown && e.character == SWT.ESC) { shell.dispose(); } if (e.type == SWT.MouseDown && e.button == 1) { Point p = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); startX = p.x - loc.x; startY = p.y - loc.y; } if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) { Point p = shell.toDisplay(e.x, e.y); p.x -= startX; p.y -= startY; shell.setLocation(p); } if (e.type == SWT.Paint) { e.gc.drawImage(image, imageData.x, imageData.y); } } }; shell.addListener(SWT.KeyDown, l); shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseMove, l); shell.addListener(SWT.Paint, l); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); image.dispose(); display.dispose(); }
public static void main(String[] args) { final Display display = new Display(); // Shell must be created with style SWT.NO_TRIM final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); shell.setBackground(display.getSystemColor(SWT.COLOR_RED)); // define a region that looks like a key hole Region region = new Region(); region.add(circle(67, 67, 67)); region.subtract(circle(20, 67, 50)); region.subtract(new int[] {67, 50, 55, 105, 79, 105}); // define the shape of the shell using setRegion shell.setRegion(region); Rectangle size = region.getBounds(); shell.setSize(size.width, size.height); // add ability to move shell around Listener l = new Listener() { /** The x/y of the MouseDown, relative to top-left of the shell. */ Point origin; @Override public void handleEvent(Event e) { switch (e.type) { case SWT.MouseDown: Point point = shell.toDisplay(e.x, e.y); Point loc = shell.getLocation(); origin = new Point(point.x - loc.x, point.y - loc.y); break; case SWT.MouseUp: origin = null; break; case SWT.MouseMove: if (origin != null) { Point p = display.map(shell, null, e.x, e.y); shell.setLocation(p.x - origin.x, p.y - origin.y); } break; } } }; shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseUp, l); shell.addListener(SWT.MouseMove, l); // add ability to close shell Button b = new Button(shell, SWT.PUSH); b.setBackground(shell.getBackground()); b.setText("close"); b.pack(); b.setLocation(10, 68); b.addListener( SWT.Selection, new Listener() { @Override public void handleEvent(Event e) { shell.close(); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); display.dispose(); }