我想创建一个“选择区域”工具。 此工具应允许使用鼠标在屏幕上绘制矩形区域。
我使用全屏,半透明,变暗的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中是否存在更有效的解决方案,而不必使用重绘方法。
修改 我使用了3张图片来进行选择:
性能是可以接受的,因为只有一个alpha混合操作(对于第二个图像)。
只剩下一个问题,当我使用shell的图形控件在第一次将alpha混合图像用作shell-background时绘制shell时,其他一切在发送鼠标事件时都有效:
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();
}
}
答案 0 :(得分:4)
尝试这种方法:
而不是shell.redraw();
,只需调用shell.print(gc)
一次,然后将GC附加到图像缓冲区。这会给你一个shell的图像。
获取SWT或jogl的OpenGL扩展。将图像放在背景中并为选择创建一个3D矩形。使用OpenGL进行alpha操作和合成。
原因是在SWT中alpha运算速度很慢,而SWT通常需要使用CPU来执行此操作。您的显卡每秒可以执行相同的操作数百次而不会出汗。