2011-05-31 75 views
7

我想畫一個SWT圖像,但似乎沒有什麼:如何繪製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(); 

我已經測試過的圖像存在,並且有內容 - 任何想法?

+0

你想在哪兒畫這幅圖像? – 2011-05-31 12:54:00

+0

在我的顯示器上。外殼打開但空了。 – u123 2011-05-31 12:56:51

+0

可能的重複[如何顯示與swt在Java中的圖像?](http://stackoverflow.com/questions/4447455/how-to-show-up-an-image-with-swt-in-java) – McDowell 2011-05-31 13:21:55

回答

8

創建一個標籤並在其上設置圖像。

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

這對你來說可能夠用了。

3

通常,使用畫布繪製圖像。

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

有關SWT圖像的更多信息,請參閱this link