簡體   English   中英

如何繪制 swt 圖像?

[英]How to draw swt image?

我正在嘗試繪制 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();

我已經測試了圖像存在並且有內容 - 有什么想法嗎?

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

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

這對你來說可能已經足夠了。

通常,使用 Canvas 來繪制圖像。

// 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 圖像的更多信息,請參閱此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM