2011-05-31 18 views
7

estoy tratando de dibujar una imagen swt pero no aparece nada:Cómo dibujar la imagen swt?

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(); 

he probado que la imagen existe y tiene el contenido - cualquier idea?

+0

¿Dónde desea dibujar esta imagen? –

+0

En mi pantalla. El shell está abierto pero vacío. – u123

+0

posible duplicado de [¿Cómo mostrar una imagen con swt en java?] (Http://stackoverflow.com/questions/4447455/how-to-show-up-an-image-with-swt-in-java) – McDowell

Respuesta

8

Crea una etiqueta y configura la imagen en ella.

Image myImage = new Image(display, "C:/sample_image.png"); 
Label myLabel = new Label(shell, SWT.NONE); 
myLabel.setImage(myImage); 

Eso puede ser suficiente para usted.

3

Normalmente, uno usa el lienzo para dibujar una imagen.

// 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);   
    } 
}); 

Consulte this link para obtener más información sobre imágenes SWT.

Cuestiones relacionadas