Export JPanel to vector graphics

I would like to export the image to my JPanel as a vector graphic file so that it can be edited and printed at a higher resolution. Essentially, I want its paint() function to be called with the target graphic, which saves the drawing commands to a vector graphic file.

What is a good, easy way to do this? What libraries are recommended? Which vector format would be best and why?

+4
source share
7 answers

See the Java EPS Graphics2D Package .

Many Java programs use Graphics2D to draw material on the screen, and although it is easy to save the result as a png or jpeg file, it is a little more difficult to export it as an EPS for inclusion in a document or document.

This package makes the whole process extremely simple, because you can use the EpsGraphics2D object as if it were a Graphics2D object. The only difference is that all the methods implemented create an EPS output, which means that the diagrams you draw can be changed without leading to any of the jagged edges that you can see when resizing images based on pixels, such as JEPG and PNG files.

+6
source

Apache Batik allows you to draw a specialized implementation of a Graphics2D object and then export it as a scalable vector graphic (.svg) file. Then you can view / process / print it using a browser with SVG support (Firefox will process it nativly, ISTR, IE and others can use plugins).

See the SVGGraphics2D object (the process is documented here )

+5
source

EPS EPS, mentioned by Pierre, looks good, but if it’s not, you can also look at FreeHEP Vector Graphics . Written for reuse of Java in the field of high-energy physics, it includes a vector graphics package made using the Graphics2D implementation. We used it to export EPS very successfully for several years.

+2
source

I can recommend the VectorGraphics2D library (LGPL). Although it does not support all Graphics2D features, I have successfully used it for my project. It provides an implementation of java.awt.Graphics2D for various vector file formats. It simply exports all paint operations to EPS, SVG or PDF files.

+1
source

Additional libraries for people with the same requirements:

Both of these are GPLv3 and are well tested using extensive use in JFreeChart and Orson Charts .

+1
source

FreeHEP seems to work quite well, although it does not seem to be supported anymore, and its error and forum pages have disappeared. With just a few lines, you get a pop-up dialog box that can save any component in various scalable and conventional image formats. We have several complex images using alpha channel, rotated text, areas bounded by curves, and they are saved perfectly, much better with VectorGraphics2D.

The only problem I've seen so far is jpeg save, which comes out black for all my images. This is not very important for us, given that png works, plus all vector modes, but I'm sure this will be a problem for some.

I had to add this particular code to save in all of these modes:

 public static void showImage(Component comp) { try { ExportDialog export = new ExportDialog(); export.showExportDialog( null, "Export view as ...", comp, "export" ); System.err.println("Image save complete"); } catch(Exception e) { e.printStackTrace(); } } 

There are tons of libraries that should also be added.

0
source

this is mostly impossible directly, since the low-level java api works in terms of raster (pixels) and is never saved in vector format. (Check out the java.awt.Graphics API to figure out what I mean).

There is a general purpose program that converts raster to vector formats, this is the one I found during a quick search: http://autotrace.sourceforge.net/index.html

therefore, using such a program, you can divide the problem into two smaller problems:

-1
source

All Articles