Shape based of Lines and Arcs

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).

Shape based of Lines and Arcs

Postby Geduldspiele » Sat Oct 04, 2014 3:51 pm

I have a complicated shape form which is based of arcs and lines.
It is possible to connect this arcs and lines to a closed path?
I only see drawPolygon or drawPolyline . And draw(Shape)
My problem is following:
If I transfer the lines and arcs to svg,
Inkscape and other svg editors do not see it as a shape.
So as example I can not fill this shape in Inkscape like an rectangle.

Is there a possible way to append several lines and arcs to make a closed path?

Many thanks in advance,
Posts: 2
Joined: Fri Oct 03, 2014 3:36 pm

Re: Shape based of Lines and Arcs

Postby paradoxoff » Mon Oct 06, 2014 9:20 am

Have you looked at the class java.awt.geom.Path2D ? This class allows to produce a closed shape consisting of lines and arcs.
Posts: 1544
Joined: Sat Feb 17, 2007 1:51 pm

Re: Shape based of Lines and Arcs

Postby david.gilbert » Wed Oct 08, 2014 8:44 pm

Paradoxoff makes a good suggestion to study Java's Path2D class. JFreeSVG maps Path2D objects from Java2D to the equivalent Path element in SVG:

Code: Select all"<path ").append(getSVGPathData(path)).append("/>");

It is the getSVGPathData() method in JFreeSVG that does the conversion (in this code, the geomDP() method just formats the values to a fixed number of decimal places):

Code: Select all
     * Creates an SVG path string for the supplied Java2D path.
     * @param path  the path ({@code null} not permitted).
     * @return An SVG path string.
    private String getSVGPathData(Path2D path) {
        StringBuilder b = new StringBuilder("d=\"");
        float[] coords = new float[6];
        boolean first = true;
        PathIterator iterator = path.getPathIterator(null);
        while (!iterator.isDone()) {
            int type = iterator.currentSegment(coords);
            if (!first) {
                b.append(" ");
            first = false;
            switch (type) {
            case (PathIterator.SEG_MOVETO):
                b.append("M ").append(geomDP(coords[0])).append(" ")
            case (PathIterator.SEG_LINETO):
                b.append("L ").append(geomDP(coords[0])).append(" ")
            case (PathIterator.SEG_QUADTO):
                b.append("Q ").append(geomDP(coords[0]))
                        .append(" ").append(geomDP(coords[1]))
                        .append(" ").append(geomDP(coords[2]))
                        .append(" ").append(geomDP(coords[3]));
            case (PathIterator.SEG_CUBICTO):
                b.append("C ").append(geomDP(coords[0])).append(" ")
                        .append(geomDP(coords[1])).append(" ")
                        .append(geomDP(coords[2])).append(" ")
                        .append(geomDP(coords[3])).append(" ")
                        .append(geomDP(coords[4])).append(" ")
            case (PathIterator.SEG_CLOSE):
                b.append("Z ");
        return b.append("\"").toString();
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
JFreeChart Project Leader
Posts: 11600
Joined: Fri Mar 14, 2003 10:29 am

Return to JFreeSVG

Who is online

Users browsing this forum: No registered users and 1 guest