我想创建一个“选择区域”工具。 这个工具应该允许用鼠标在屏幕上绘制一个矩形区域。SWT在外壳上绘制一个“透明”矩形
我使用全屏,半透明,变暗的swt Shell
作为我的背景,在该背景上绘制了一个白色矩形来表示所选区域。
我的问题是,我没有找到一个有效的方式刷新矩形区域。 到现在为止我用过的redraw
方法,但视觉效果相当难看,甚至以为我试图重新绘制仅他所需要的面积:
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();
}
}
所以我想知道,如果一个更有效的解决方案,存在于SWT,而不必使用重绘方法。
EDIT 我使用3个图像来进行选择工作:
- 首先是画面的图像(屏幕截图)
- 第二个是屏幕+ alpha混合深色矩形的图像
- 第三个是我在其上绘制alpha混合图像的缓冲区+从截图图像复制的矩形。
由于只有一个alpha混合操作(对于第二个图像),性能是可以接受的。
只有一个问题仍然存在,当我第一次使用外壳的图形控件绘制外壳时,第一次将alpha混合图像用作外壳背景时,在发送鼠标事件时其他所有内容都正常工作:
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();
}
}
我更喜欢使用print()方法,而不是OpenGl扩展或jogl。我会尝试shell.print(gc)。 – zeropouet 2012-03-06 15:53:28
您可以尝试使用该图像来加速重绘,但我的直觉是SWT alpha实现对于全屏操作而言太慢。 – 2012-03-06 16:44:45