I am trying to draw an swt Image but nothing appears:
Display display = new Display();
Shell shell = new Shell(display);
shell.open();
Image image = new Image(display, "C:/sample_image.png");
Rectangle bounds = image.getBounds();
GC gc = new GC(image);
gc.drawImage(image, 100, 100);
// gc.drawLine(0, 0, bounds.width, bounds.height);
// gc.drawLine(0, bounds.height, bounds.width, 0);
// gc.dispose();
// image.dispose();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
I have tested that the image exists and has content - any ideas?
Create a Label and set the image on it.
Image myImage = new Image( display, "C:/sample_image.png" );
Label myLabel = new Label( shell, SWT.NONE );
myLabel.setImage( myImage );
That may be enough for you.
Usually, one uses the Canvas to draw an image.
// Create a canvas
Canvas canvas = new Canvas(shell, SWT.NONE);
final Image image = new Image(display, "C:/sample_image.png");
// Create a paint handler for the canvas
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(image, 0, 0);
}
});
See this link for more info on SWT Images.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With