Reputation: 817
I've just started working with swt's canvas. I'm under the impression that draw commands from the graphics controller (gc) can be executed in any listener that is attached to the canvas. I guess though, my question is what triggers the paint listener method paintControl
?
Does the canvas.redraw()
cause this to run as well?
Thanks for anyone who can point me in the right direction. If there are tutorials or books I can read on the matter(2d drawing in swt in general as well), I would love to hear of them.
EDIT: Here is the code, I'm running into an issue where the graphCanvas is not filling excess vertical space? It doesn't work when I have it fill excessHorizontal space either. But it seems to work when I apply those properties to the composites. Any idea?
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);
Upvotes: 2
Views: 6713
Reputation: 671
Your code can definitely call canvas.redraw() manually, which will then trigger a paintControl event. SWT itself will also cause repaints to be triggered as needed, for things like window resizing and when the canvas gets obscured/un-obscured by other windows.
Upvotes: 1