项目需求,要把GEF模型在editor中的显示保存为一张图片,便于预览或在文档中使用。研究了GMF的代码,它使用AWT来进行图片的输出,所以在中间作了一个SWT和AWT的转换Adapter. 是一个叫做AWTGraphics的类.其实使用SWT本身就可以进行。
使用draw2D提供的SWTGraphics可以进行GEG图像的保存。本程序假定读者对GEF有一定了解。

privatevoidsaveOutlinePicture(ScrollingGraphicalViewerviewer)...{
LayerManagerlayerManager=(LayerManager)viewer.getEditPartRegistry().get(LayerManager.ID);
//saveimageusingswt
//getrootfigure
// 获取显示在editor中的背景层和打印层
IFigurebackgroundLayer=layerManager.getLayer(LayerConstants.GRID_LAYER);

IFigurecontentLayer=layerManager.getLayer(LayerConstants.PRINTABLE_LAYERS);

//createimagefromrootfigure
Imageimg=newImage(null,contentLayer.getSize().width,contentLayer.getSize().height);
GCgc=newGC(img);
Graphicsgraphics=newSWTGraphics(gc);
graphics.translate(contentLayer.getBounds().getLocation());
// 传送显示图层
backgroundLayer.paint(graphics);
contentLayer.paint(graphics);
graphics.dispose();
gc.dispose();

//saveimagetofile
ImageLoaderil=newImageLoader();

il.data=newImageData[]...{img.getImageData()};
//保存地址和保存图片的类型
il.save(“c:/test.jpg",SWT.IMAGE_JPEG);
}
使用draw2D提供的SWTGraphics可以进行GEG图像的保存。本程序假定读者对GEF有一定了解。





// 获取显示在editor中的背景层和打印层









// 传送显示图层









//保存地址和保存图片的类型

