
I can't get a canvas to draw on a composite with SWT. I've made the composite on a shell and given it a layout but the canvas won't draw the image. Please help me and bare with me because I'm new to java. The relevant code is below.
public static void main(String[] args) {
final Display display = new Display();
final Image img = new Image(display,"C:\\Users\\Public\\Pictures\\Sample
Pictures\\Lighthouse.jpg");
final Shell shell = new Shell(display);
shell.setText("Canvas in Composite");
Composite comp = new Composite(shell, SWT.BORDER);
comp.setBounds(0, 0, 500, 500);
comp.setLayout(new FillLayout());
final Canvas can = new Canvas(comp, SWT.NONE);
can.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(img, 0, 0);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
img.dispose();
display.dispose();
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.