在Shell上绘制“透明”的矩形

4
我希望能创建一个“选择区域”工具。这个工具应该允许使用鼠标在屏幕上绘制一个矩形区域。
我正在使用全屏、半透明、暗色的 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黑色矩形的图像
第三个是缓冲区,在其中绘制了从截图图像复制的alpha混合图像和矩形。
性能是可以接受的,因为只有一个alpha混合操作(第二个图像)。
只有一个问题尚未解决,当我使用shell的图形控件第一次绘制shell时,无法将alpha混合图像用作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();
    }
}
1个回答

4
尝试这种方法:
  • 不要使用shell.redraw();,而是调用shell.print(gc)一次,并将GC附加到图像缓冲区。这给你一个shell的图像。

  • 获取SWT或jogl的OpenGL扩展。将图像放在背景中,并为所选内容创建一个3D矩形。使用OpenGL进行Alpha操作和合成。

推理是,在SWT中Alpha操作很慢,通常必须使用CPU来完成它。你的显卡可以在几百次每秒的速度下轻松完成同样的操作。

我更喜欢使用print()方法而不是OpenGl扩展或jogl。我会尝试使用shell.print(gc)。 - zeropouet
你可以尝试使用图像来加速重绘,但我的直觉是SWT alpha实现对于全屏操作来说太慢了。 - Aaron Digulla

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接