最近我在用GEF的一个图形编辑框架,很惭愧,自己Java还半生不熟,就直接被拉来做项目。
闲话短说,在项目进行过程中,涉及到这样的功能:将自己的Editor中的内容保存成图片。
我在网上找了一些资料,但是说得不够具体,可能对于像我这种Java菜鸟,又刚开始接触GEF的不大容易理解,通过自己的摸索,终于把图片导出来了,以下是我的实现过程:
1.首先要写一个创建图片文件的方法createImage(),这个我也是从网上摘抄来的,感谢网友的无私支持:
摘自[url]http://tjlvan.iteye.com/blog/226627[/url]
2.然后是第二步,也是之前我在网上找资料时比较迷惑不解的地方,即这个方法在什么地方使用? 我是这样实现的:
在我们自己创建的Editor的那个类中来用它。不管是RCP还是在Eclipse IDE中GEF应该都有一个自己的Editor(比如我的是MyFrameDeployEditor),即程序运行时那个空白编辑区(也叫“画板”),我在这个类的doSave()中使用以上方法,在保存文件为xml或者序列化的同时将画板上的内容另保存成图片文件。具体代码如下:
闲话短说,在项目进行过程中,涉及到这样的功能:将自己的Editor中的内容保存成图片。
我在网上找了一些资料,但是说得不够具体,可能对于像我这种Java菜鸟,又刚开始接触GEF的不大容易理解,通过自己的摸索,终于把图片导出来了,以下是我的实现过程:
1.首先要写一个创建图片文件的方法createImage(),这个我也是从网上摘抄来的,感谢网友的无私支持:
/**
* Returns the bytes of an encoded image for the specified
* IFigure in the specified format.
*
* @param figure the Figure to create an image for.
* @param format one of SWT.IMAGE_BMP, SWT.IMAGE_BMP_RLE,SWT.IMAGE_GIF
* SWT.IMAGE_ICO, SWT.IMAGE_JPEG, or SWT.IMAGE_PNG
* @return the bytes of an encoded image for the specified Figure
*/
private byte[] createImage(IFigure figure, int format) {
Device device = getEditPartViewer().getControl().getDisplay();
Rectangle r = figure.getBounds();
ByteArrayOutputStream result = new ByteArrayOutputStream();
Image image = null;
GC gc = null;
Graphics g = null;
try {
image = new Image(device, r.width, r.height);
gc = new GC(image);
g = new SWTGraphics(gc);
g.translate(r.x * -1, r.y * -1);
figure.paint(g);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {image.getImageData()};
imageLoader.save(result, format);
} finally {
if (g != null) {
g.dispose();
}
if (gc != null) {
gc.dispose();
}
if (image != null) {
image.dispose();
}
}
return result.toByteArray();
}
摘自[url]http://tjlvan.iteye.com/blog/226627[/url]
2.然后是第二步,也是之前我在网上找资料时比较迷惑不解的地方,即这个方法在什么地方使用? 我是这样实现的:
在我们自己创建的Editor的那个类中来用它。不管是RCP还是在Eclipse IDE中GEF应该都有一个自己的Editor(比如我的是MyFrameDeployEditor),即程序运行时那个空白编辑区(也叫“画板”),我在这个类的doSave()中使用以上方法,在保存文件为xml或者序列化的同时将画板上的内容另保存成图片文件。具体代码如下:
public class MyFrameDeployEditor extends GraphicalEditorWithPalette {
...
public void doSave(IProgressMonitor monitor) {
//...这里将GEF项目保存成XML文件或者序列化为二进制文件或者可以什么都不做
//以下为保存图片的代码,弹出一个对话框,定义好路径和名字后,调用以上创建图片文件的方法,即可保存成图片。
FileDialog dialog = new FileDialog(getSite().getWorkbenchWindow().getShell(), SWT.SAVE);
dialog.setText("保存图片文件");
dialog.setFilterNames(new String[] { "PNG(*.png)" });
dialog.setFilterExtensions(new String[] { "*.png", "*.*" });
String fileName = dialog.open();
ScalableRootEditPart rootPart = (ScalableRootEditPart)this.getGraphicalViewer().getRootEditPart();//这里你根据你自己的Editor设置的是什么RootEditPart类型就定义什么类型即可,我这里是ScalableRootEditPart
IFigure figure = rootPart.getLayer(ScalableRootEditPart.PRINTABLE_LAYERS);
byte[] data = createImage(figure, SWT.IMAGE_PNG); //调用createImage()方法
try {
FileOutputStream fos = new FileOutputStream(fileName);
fos.write(data);
fos.close();
MessageDialog.openInformation(this.getSite().getShell(), "导出",
"图形已经导出到 " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
...
}