2

我刚刚开始使用 swt 的画布。我的印象是,来自图形控制器 (gc) 的绘制命令可以在任何连接到画布的侦听器中执行。不过我想,我的问题是什么触发了绘画侦听器方法paintControl

这是否canvas.redraw()也会导致它运行?

感谢任何可以为我指明正确方向的人。如果有关于此事的教程或书籍我可以阅读(一般也可以在 swt 中进行二维绘图),我很想听听它们。

编辑: 这是代码,我遇到了一个问题,即 graphCanvas 没有填充多余的垂直空间?当我让它填充多余的水平空间时它也不起作用。但是当我将这些属性应用于复合材料时,它似乎起作用了。任何的想法?

Color black = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
    chartComposite = new Composite(parent, SWT.NONE);
    GridLayout chartGridLayout = new GridLayout(1,false);
    GridData chartGridData = new GridData();
    chartGridData.grabExcessHorizontalSpace = true;
    chartGridData.grabExcessVerticalSpace = true;

    chartComposite.setLayout(chartGridLayout);
    chartComposite.setLayoutData(chartGridData);

    chartInnerComposite = new Composite(chartComposite, SWT.NONE);
    GridLayout chartInnerGridLayout = new GridLayout(2,false);
    GridData chartInnerGridData = new GridData();
    chartInnerGridData.grabExcessHorizontalSpace = true;
    chartInnerGridData.grabExcessVerticalSpace = true;
    chartInnerComposite.setLayout(chartInnerGridLayout);
    chartInnerComposite.setLayoutData(chartInnerGridData);
    /**
     * Create the four canvases
     */

    Canvas yCanvas = new Canvas(chartInnerComposite, SWT.None);
    graphCanvas = new Canvas(chartInnerComposite, SWT.None);
    GridLayout graphGridLayout = new GridLayout(1, false);
    graphGridLayout.makeColumnsEqualWidth = false;
    GridData graphGridData = new GridData(SWT.FILL);
    graphGridData.widthHint = 400;
    graphGridData.grabExcessVerticalSpace = true;
    graphCanvas.setLayout(graphGridLayout);
    graphCanvas.setLayoutData(graphGridData);
    graphCanvas.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            e.gc.drawLine(20, 20, 500, 500);
            e.gc.drawRectangle(0, 0, graphCanvas.getSize().x -1, graphCanvas.getSize().y -1);

            e.gc.drawLine(0, 40, 500, 500);
            e.gc.drawLine(0, 0, 0, 1200);
            e.gc.drawLine(0, 0, 1200, 0);               
        }
    });
    /*graphCanvas.setSize(400, 400);
    graphCanvas.redraw();
    graphCanvas.update();*/


    Canvas filler = new Canvas(chartInnerComposite, SWT.NONE);
    Canvas xCanvas = new Canvas(chartInnerComposite, SWT.None);
4

1 回答 1

1

您的代码绝对可以手动调用 canvas.redraw(),然后触发一个paintControl 事件。SWT 本身也会根据需要触发重绘,例如调整窗口大小以及当画布被其他窗口遮挡/不遮挡时。

于 2012-08-23T15:50:33.310 回答