u123
u123

Reputation: 16329

How to draw swt image?

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?

Upvotes: 8

Views: 9782

Answers (2)

Sandman
Sandman

Reputation: 9692

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.

Upvotes: 4

Mario Marinato
Mario Marinato

Reputation: 4617

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.

Upvotes: 9

Related Questions