Export JPanel to vector graphics
Asked Answered
I

7

6

I would like to export the image in my JPanel to a vector graphics file so it can be edited and printed at a higher-than-screen resolution. Essentially I want its paint() function to be called with a destination Graphics that saves the drawing commands into a vector graphic file.

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

Illogicality answered 1/5, 2009 at 15:40 Comment(0)
B
7

Have a look at The Java EPS Graphics2D package.

Many Java programs use Graphics2D to draw stuff on the screen, and while it is easy to save the output as a png or jpeg file, it is a little harder to export it as an EPS for including in a document or paper.

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

Burson answered 1/5, 2009 at 15:56 Comment(2)
That looks very nice; thanks. I'm not sure EPS is right for me, but I may go that route and if I do, this certainly looks like a good package for it.Illogicality
I'm going to go with this solution; thank you. It seems that EPS will serve nicely.Illogicality
J
6

Apache Batik will let you paint to a specialised implementation of a Graphics2D object and then export as an scalable vector graphics (.svg) file. You can then view/process/print it using an SVG-enabled browser (Firefox will handle it nativly, ISTR, IE and others can use plugins).

See the SVGGraphics2D object (process documented here)

Jadejaded answered 1/5, 2009 at 15:46 Comment(3)
I had forgotten about Batik; thanks. I've seen it before, and I think SVG might be a good format for me.Illogicality
If I could pick two answers, I'd pick this one, too; it's just that EPS seems like a better solution for my needs than SVG.Illogicality
Just wanted to mention that Apache SVG has issues with exporting gradients (they are not exported correctly). Also if a graphics itself uses SVG, then the export is broken. AFAIK SVGSalamander does not have the option to write to SVG.Cringe
G
3

The Java EPS mentioned by Pierre looks good, but if it isn't you might also like to look at FreeHEP Vector Graphics. Written to allow Java reuse in the High Energy Physics field it includes a vector graphics package, done through an implementation of Graphics2D. We've used it to export EPS very successfull for a number of years.

Generalization answered 1/5, 2009 at 17:29 Comment(1)
Thank you; I wound up using FreeHEP Vector Graphics and found it quite easy to use and pretty good. Convenient and well documented.Illogicality
H
2

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

Headwork answered 1/11, 2010 at 11:26 Comment(0)
A
2

Additional libraries for people with the same requirement:

Both of these GPLv3 and well tested via extensive usage in JFreeChart and Orson Charts.

Aulic answered 29/9, 2015 at 13:54 Comment(0)
P
1

FreeHEP seems to work quite well although it does not appear to be maintained anymore and its bug and forum pages are gone. With just a handful of lines you get a popup dialog that can save any component to a variety of scalable and regular image formats. We have some challenging images, using alpha channel, rotated text, areas bounded by curves, and they saved perfectly, much better with with VectorGraphics2D.

The only problem I've seen so far is in jpeg save, which comes out black for all my images. This isn't very important for us given that png works, plus all the vector modes, but I'm sure it would be an issue for some.

I had to add exactly this much code to save in all 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 a bunch of library jars that must be added as well.

Panettone answered 5/2, 2014 at 21:33 Comment(0)
F
0

this is basically not possible directly, as the low level java api works in terms of raster (pixels) and is never stored in vector format. (Check the API of java.awt.Graphics to see what I mean).

there are some general purpose program that convert raster to vector formats, this is one I found on a quick search: http://autotrace.sourceforge.net/index.html

so, using such a program you can divide your problem into two smaller problems:

  1. convert your JPanel into a bitmap or file (http://www.jguru.com/faq/view.jsp?EID=242020)
  2. run autotrace on the file.
Fleischer answered 1/5, 2009 at 15:47 Comment(2)
You can do this, since the API will talk in terms of drawRectangle() etc. That gets rendered in different ways depending on the component you're drawing toJadejaded
When you call drawLine() on the specialised Graphics2D implementation it writes the vector representation of that line to the file.Generalization

© 2022 - 2024 — McMap. All rights reserved.