I would like to create a “selection area” tool.
This tool should allow to draw a rectangle area on the screen with the mouse.
I am using a fullscreen, translucent, darken swt Shell as my background on which I draw a white rectangle to represent the selected area.
My problem is that I did not find an efficient way to refresh the rectangle area.
Until now I’ve used the redraw method but the visual effect is quite ugly, even thought I tried to redraw only he needed area :
public ManualScreenAreaSelector(final Display display) {
shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP);
shell.setBounds(display.getClientArea());
// shell.setFullScreen(true);
shell.setAlpha(180);
shell.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
shell.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
}
@Override
public void mouseMove(final MouseEvent e) {
if (editionMode) {
// retrieve the rectangular area corresponding to mouse selection
final Rectangle r = makeRectangleFromSelection(clickCoordinates, new Point(e.x, e.y));
// make the ugly 'tint' effect
shell.redraw();
GC gc = new GC(shell);
gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WHITE));
gc.setAlpha(150);
gc.fillRectangle(r.x, r.y, r.width, r.height);
gc.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
gc.fillRectangle(0, 0, r.x, r.y);
gc.fillRectangle(0, 1080 - r.y, r.x, 1080 - r.y);
gc.dispose();
lastX = e.x;
lastY = e.y;
}
}
@Override
public void mouseDown(final MouseEvent e) {
// Right click = reset selection
if (e.button == 3) {
shell.redraw();
selectedArea = null;
if (editionMode) {
editionMode = false;
shell.removeMouseMoveListener(ManualScreenAreaSelector.this);
}
} else if (e.button == 1) {
// left-click enter edition mode
// Reset previous selection
selectedArea = null;
editionMode = true;
clickCoordinates = new Point(e.x, e.y);
lastX = e.x;
lastY = e.y;
shell.addMouseMoveListener(ManualScreenAreaSelector.this);
}
}
@Override
public void mouseUp(final MouseEvent e) {
// left click, only if edition was set
if ((e.button == 1) && editionMode) {
editionMode = false;
shell.removeMouseMoveListener(ManualScreenAreaSelector.this);
selectedArea = makeRectangleFromSelection(clickCoordinates, new Point(e.x, e.y));
shell.dispose();
}
}
So I would like to know if a more efficient solution, exist in SWT, without having to use the redraw method.

EDIT
I used 3 images to make the selection work :
- The first is the image of the screen (a screenshot)
- The second is the image of the screen + alpha-blended dark rectangle
- The third is a buffer on which I draw the alpha-blended image + the rectangle copied from the screenshot image.
Performance is acceptable since there is only one alpha blend operation (for the second image).
Only one problem remains, when I used the graphics control of the shell to draw the shell the first time the alpha-blended image is not used as shell-background, everything else is working when the mouse events are sent :
public ManualScreenAreaSelector(final Display display) {
screenWidth = display.getClientArea().width;
screenHeight = display.getClientArea().height;
// create a new Image of the screen
backGround = new Image(display, display.getBounds());
GC gc = new GC(display);
gc.copyArea(backGround, 0, 0);
gc.dispose();
// Copy background image and add alpha blended effect
aplhaBackGround = new Image(backGround.getDevice(), backGround.getImageData());
GC alphaGC = new GC(aplhaBackGround);
alphaGC.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
alphaGC.setAlpha(200);
alphaGC.fillRectangle(0, 0, screenWidth, screenHeight);
alphaGC.dispose();
// create the shell
shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP | SWT.NO_BACKGROUND);
shell.setBounds(display.getClientArea());
// get shell graphics control
shellGraphics = new GC(shell);
// set the shell image to screen image <-- does nothing
shellGraphics.drawImage(aplhaBackGround, 0, 0);
// Image for the shell
bufferImage = new Image(shell.getDisplay(), shell.getBounds());
shell.print(shellGraphics);
}
public void mouseMove(final MouseEvent e) {
if (editionMode) {
// Get selected area
final Rectangle selectedArea = makeRectangleFromSelection(clickCoordinates, new Point(
e.x, e.y));
// Copy alpha blended background into the buffer
GC gc1 = new GC(aplhaBackGround);
gc1.copyArea(bufferImage, 0, 0);
gc1.dispose();
// Paint "normal" background over selected area
GC gc2 = new GC(bufferImage);
gc2.drawImage(backGround, selectedArea.x, selectedArea.y, selectedArea.width,
selectedArea.height, selectedArea.x, selectedArea.y, selectedArea.width,
selectedArea.height);
// draw the painted image on the shell
shellGraphics.drawImage(bufferImage, 0, 0);
gc2.dispose();
}
}
Try this approach:
Instead of
shell.redraw();, callshell.print(gc)once and attach the GC to an image buffer. That gives you an image of the shell.Get the OpenGL extensions for SWT or jogl. Put the image in the background and create a 3D rectangle for the selection. Use OpenGL for the alpha operations and composition.
The reasoning is that alpha operations are slow in SWT which often has to use the CPU to do it. Your graphics card can do the same operation hundreds of times per second without breaking a sweat.