Java的SWT:GC不会在画布上绘制简单的矩形,但会在图像上绘制它

时间:2015-05-06 09:17:13

标签: java swt

我无法获取GC(图形上下文)对象在画布上绘制一个简单的矩形,我无法弄清楚我错过了什么......

考虑这个简单的代码片段:

public class MainWindow {

    private Display display;
    private Shell shell;

    public static void main(String args[]) {
        MainWindow mw=new MainWindow();
        mw.render();
    }

    public MainWindow() {

        display=Display.getDefault();
        shell=new Shell(display, SWT.DIALOG_TRIM);

        shell.setText("Graphic Test");
        shell.setSize(400,300);
        shell.setLocation(0, 0);

        GC gc=new GC(shell);
        gc.drawRectangle(50,50,45,45);
        gc.dispose();


    }

    public void render() {

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

}

这不会画任何东西:
enter image description here

这也不会:

public class MainWindow {

    private Display display;
    private Shell shell;

    public static void main(String args[]) {
        MainWindow mw=new MainWindow();
        mw.render();
    }

    public MainWindow() {

        display=Display.getDefault();
        shell=new Shell(display, SWT.DIALOG_TRIM);

        shell.setText("Graphic Test");
        shell.setSize(400,300);
        shell.setLocation(0, 0);

        Canvas canvas=new Canvas(shell, SWT.NONE);
        canvas.setSize(shell.getSize().x, shell.getSize().y);

        GC gc=new GC(canvas);
        gc.drawRectangle(50,50,45,45);
        gc.dispose();


    }

    public void render() {

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

}

enter image description here

但请注意当我使用背景图像设置画布并在图像上绘画时会发生什么:

public class MainWindow {

    private Display display;
    private Shell shell;

    public static void main(String args[]) {
        MainWindow mw=new MainWindow();
        mw.render();
    }

    public MainWindow() {

        display=Display.getDefault();
        shell=new Shell(display, SWT.DIALOG_TRIM);

        shell.setText("Graphic Test");
        shell.setSize(400,300);
        shell.setLocation(0, 0);

        Image background=new Image(display, "background.jpg");

        Canvas canvas=new Canvas(shell, SWT.NONE);
        canvas.setBackgroundImage(background);
        canvas.setSize(shell.getSize().x, shell.getSize().y);

        GC gc=new GC(background);
        gc.drawRectangle(50,50,45,45);
        gc.dispose();


    }

    public void render() {

        shell.open();

        while (!shell.isDisposed()) {

            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

        display.dispose();
    }

}

enter image description here

突然间,奇迹出现了。它 绘制矩形 因此,绘制图像没有问题,但在shell / canvas上绘图不会产生任何东西......

我在这里缺少什么?难道我不能画任何Drawable吗?

1 个答案:

答案 0 :(得分:1)

这是一个时间问题(在{1}期间CanvasShell的绘图将在后续的绘画过程中被过度绘制,而在Image上绘制的内容将持续存在于其中ImageData)。对于Canvas(或Shell),您应该在其绘制事件期间绘制,如tutorial中所示。 PaintEvent带来了自己的GC,这是管理您的绘图操作的正确方法:

final Canvas canvas=..;
canvas.addPaintListener(new PaintListener() { 
    public void paintControl(PaintEvent e) { 
        e.gc.drawRectangle(50,50,45,45);
    }
});

enter image description here