我刚刚开始使用swt的画布。我的印象是来自图形控制器(gc)的绘制命令可以在任何附加到画布的侦听器中执行。我想,我的问题是什么触发了绘图监听器方法paintControl
?
canvas.redraw()
导致它也能运行吗?
感谢任何能指出我正确方向的人。如果有关于这个问题的教程或书籍(一般来说也是2d),我很想听听它们。
修改 这是代码,我遇到的问题是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);
答案 0 :(得分:1)
您的代码绝对可以手动调用canvas.redraw(),然后触发paintControl事件。 SWT本身也会根据需要触发重新绘制,例如窗口调整大小以及画布被其他窗口遮挡/遮挡的情况。