Home » Eclipse Projects » GEF » Export to bmp problem: connections issue
Export to bmp problem: connections issue [message #232996] |
Sun, 15 April 2007 03:03  |
Eclipse User |
|
|
|
Hi,
I draw a simple diagram (2 RectangleFigures and 1 PolylineConnection) with
Draw2d and want to export this diagram to a bmp file. In application it
displays well but in exported file connection figure is not properly
displayed. How to properly deal with the connection?
Here are the codes, the createImage() method is from a post in newsgroup
( http://dev.eclipse.org/newslists/news.eclipse.tools.gef/msg0 5012.html):
public static void main(String[] args) {
Display d = new Display();
final Shell shell = new Shell(d);
shell.setSize(400, 400);
shell.setText("Test");
LightweightSystem lws = new LightweightSystem(shell);
Figure contents = new Figure();
contents.setSize(800, 500);
XYLayout contentsLayout = new XYLayout();
contents.setLayoutManager(contentsLayout);
RectangleFigure r1 = new RectangleFigure();
contents.add(r1);
r1.setBounds(new Rectangle(10, 10, 60, 60));
RectangleFigure r2 = new RectangleFigure();
contents.add(r2);
r2.setBounds(new Rectangle(300, 200, 100, 100));
PolylineConnection conn = new PolylineConnection();
ChopboxAnchor sourceAnchor = new ChopboxAnchor(r1);
ChopboxAnchor targetAnchor = new ChopboxAnchor(r2);
conn.setSourceAnchor(sourceAnchor);
conn.setTargetAnchor(targetAnchor);
contents.add(conn);
byte[] data = createImage(d, contents, SWT.IMAGE_BMP);
FileOutputStream fos;
try {
fos = new FileOutputStream("C:\\test.bmp");
fos.write(data);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
lws.setContents(contents);
shell.open();
while (!shell.isDisposed())
while (!d.readAndDispatch())
d.sleep();
}
private static byte[] createImage(Display display, 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(display, 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();
}
Attachment: shouldbe.gif
(Size: 6.76KB, Downloaded 143 times)
Attachment: bmpfile.gif
(Size: 1.92KB, Downloaded 158 times)
|
|
|
Re: Export to bmp problem: connections issue [message #233399 is a reply to message #232996] |
Tue, 24 April 2007 08:24  |
Eclipse User |
|
|
|
Hi Hao,
Move your createImage() call after shell.open() and your image is captured
correctly.
Note I had to set the shell size to match the contents size (800, 500) or
the image is clipped.
Cheers...
Anthony
"Hao Zhang" <bjzhanghao@21cn.com> wrote in message
news:evsipa$pgj$1@build.eclipse.org...
> Hi,
> I draw a simple diagram (2 RectangleFigures and 1 PolylineConnection) with
> Draw2d and want to export this diagram to a bmp file. In application it
> displays well but in exported file connection figure is not properly
> displayed. How to properly deal with the connection?
>
> Here are the codes, the createImage() method is from a post in newsgroup
> ( http://dev.eclipse.org/newslists/news.eclipse.tools.gef/msg0 5012.html):
>
> public static void main(String[] args) {
> Display d = new Display();
> final Shell shell = new Shell(d);
> shell.setSize(400, 400);
> shell.setText("Test");
> LightweightSystem lws = new LightweightSystem(shell);
> Figure contents = new Figure();
> contents.setSize(800, 500);
> XYLayout contentsLayout = new XYLayout();
> contents.setLayoutManager(contentsLayout);
>
> RectangleFigure r1 = new RectangleFigure();
> contents.add(r1);
> r1.setBounds(new Rectangle(10, 10, 60, 60));
>
> RectangleFigure r2 = new RectangleFigure();
> contents.add(r2);
> r2.setBounds(new Rectangle(300, 200, 100, 100));
>
> PolylineConnection conn = new PolylineConnection();
> ChopboxAnchor sourceAnchor = new ChopboxAnchor(r1);
> ChopboxAnchor targetAnchor = new ChopboxAnchor(r2);
> conn.setSourceAnchor(sourceAnchor);
> conn.setTargetAnchor(targetAnchor);
> contents.add(conn);
>
> byte[] data = createImage(d, contents, SWT.IMAGE_BMP);
> FileOutputStream fos;
> try {
> fos = new FileOutputStream("C:\\test.bmp");
> fos.write(data);
> } catch (FileNotFoundException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> }
>
>
> lws.setContents(contents);
> shell.open();
> while (!shell.isDisposed())
> while (!d.readAndDispatch())
> d.sleep();
>
> }
>
>
> private static byte[] createImage(Display display, 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(display, 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();
> }
>
>
>
|
|
|
Goto Forum:
Current Time: Sun Jul 13 16:10:56 EDT 2025
Powered by FUDForum. Page generated in 0.02877 seconds
|