java - Java swt画布如何工作?

  显示原文与译文双语对照的内容
0 0

我刚开始使用swt的canvas 。 我认为从图形控制器( gc ) 中绘制命令可以以在附加到画布的任何侦听器中执行。 我想,我的问题是什么触发了油漆监听器方法 paintControl

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

感谢任何可以向我指出正确的direction的人。 如果有教程或者书,我可以以阅读关于( 一般情况下,在swt中绘制),我希望听到它们。

这是代码,我正遇到一个问题,其中graphCanvas不填充多余的垂直空间? 当我把它填充excessHorizontal空间时它就不工作了。 但是当我将这些特性应用到复合材料中时,它似乎工作起来。 任何想法?

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);
时间:原作者:5个回答

0 0

代码可以手动调用 canvas.redraw(),这将触发paintControl事件。 SWT本身也会导致重新触发重新绘制,如窗口调整大小和画布被遮盖。

原作者:
...