画布有半透明背景

时间:2017-11-30 10:55:18

标签: java swt

我想创建一个半透明背景的画布。所以我尝试过这样的事情:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Main {

    public static int WIDTH=500;
    public static int HEIGHT=70;
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell(display);
        shell.setSize(WIDTH,HEIGHT);
        shell.setLayout(new GridLayout());
        shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
        create(shell);


        shell.pack();
        shell.open ();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }


    private static void create(Shell shell){
        Canvas c= new Canvas(shell, SWT.DOUBLE_BUFFERED);
        GridData gd= new GridData();
        gd.heightHint=HEIGHT-30;
        gd.widthHint=WIDTH-30;
        c.setLayoutData(gd);
        c.setBackgroundMode(SWT.INHERIT_FORCE);
        c.setBackground(new Color(shell.getDisplay(), 255, 255, 255, 100));
    }

}

但我用于画布背景的颜色(100)的alpha部分似乎没有被SWT考虑,除非我把0,如果我使用0,画布是完全透明的,在所有其他情况如果不透明。 我在这里犯了一些错误,还是SWT问题?

我在Windows 8上使用SWT。

1 个答案:

答案 0 :(得分:0)

正如greg-449所指出的,这是SWT for windows环境中的一个错误。我在SWT中打开了一个缺陷:https://bugs.eclipse.org/bugs/show_bug.cgi?id=528249

作为一种解决方法,我们总是可以将GC上的aplha设置为自己绘制画布,这种方法适用于所有环境:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Main {

    public static int WIDTH=500;
    public static int HEIGHT=70;
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell(display);
        shell.setSize(WIDTH,HEIGHT);
        shell.setLayout(new GridLayout());
        shell.setBackground(display.getSystemColor(SWT.COLOR_RED));
        create(shell);


        shell.pack();
        shell.open ();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }


    private static void create(Shell shell){
        Canvas c= new Canvas(shell, SWT.DOUBLE_BUFFERED);
        GridData gd= new GridData();
        gd.heightHint=HEIGHT-30;
        gd.widthHint=WIDTH-30;
        c.setLayoutData(gd);
        c.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TRANSPARENT));
        c.setBackgroundMode(SWT.INHERIT_FORCE);
        c.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                Rectangle clientArea = c.getClientArea();
                e.gc.setAlpha(100);
                e.gc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
                e.gc.fillRectangle(clientArea.x,clientArea.y,clientArea.width-1,clientArea.height-1);
            }
        });
    }

}