Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw swt image?

Tags:

java

swt

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?

like image 808
u123 Avatar asked May 31 '11 12:05

u123


2 Answers

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.

like image 55
Mario Marinato Avatar answered Oct 21 '22 05:10

Mario Marinato


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.

like image 37
Sandman Avatar answered Oct 21 '22 06:10

Sandman