如何绘制swt图像?

时间:2011-05-31 12:27:33

标签: java 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();

我测试过图片存在并且有内容 - 有什么想法吗?

2 个答案:

答案 0 :(得分:8)

创建标签并在其上设置图像。

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

这对你来说已经足够了。

答案 1 :(得分:4)

通常,人们使用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图像的详细信息,请参阅this link