Shape based of Lines and Arcs

A discussion forum for JFreeSVG (a fast, lightweight, SVG generator for the Java platform).
Locked
Geduldspiele
Posts: 2
Joined: Fri Oct 03, 2014 3:36 pm
antibot: No, of course not.

Shape based of Lines and Arcs

Post by Geduldspiele » Sat Oct 04, 2014 3:51 pm

Hello,
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,
Geduldspiele

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Shape based of Lines and Arcs

Post by 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.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Shape based of Lines and Arcs

Post by 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

this.sb.append("<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(" ")
                        .append(geomDP(coords[1]));
                break;
            case (PathIterator.SEG_LINETO):
                b.append("L ").append(geomDP(coords[0])).append(" ")
                        .append(geomDP(coords[1]));
                break;
            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]));
                break;
            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(" ")
                        .append(geomDP(coords[5]));
                break;
            case (PathIterator.SEG_CLOSE):
                b.append("Z ");
                break;
            default:
                break;
            }
            iterator.next();
        }  
        return b.append("\"").toString();
    }
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked