Skip to main content

GraphicsPath Class

A sequence of connected lines and curves that can be filled or stroked.

Declaration

public class GraphicsPath extends JavaObject implements ICloneable

Remarks

Create a PathFragment object to define a shape and its appearance. Use GraphicsPath to specify path geometry.

The following code snippet adds a triangle to a page:

import com.devexpress.docs.pdf.*;
import com.devexpress.drawing.*;
import com.devexpress.system.drawing.*;
import com.devexpress.drawing.printing.*;

import java.nio.channels.*;
import java.nio.file.*;

public class Main {
    public static void main(String[] args) throws Exception {

        // Create a new PDF document.
        try (PdfDocument pdfDocument = new PdfDocument()) {

            // Add a Letter-size page to the document.
            Page page = pdfDocument.getPages().add(DXPaperKind.LETTER);

            // Create a triangle geometry.
            GraphicsPath triangle = new GraphicsPath(new PointF(50, 500));
            triangle.appendLineSegment(new PointF(50, 300));
            triangle.appendLineSegment(new PointF(250, 300));
            triangle.setClosed(true);

            // Create a path fragment for a triangle shape.
            PathFragment triangleFragment = new PathFragment();

            // Assign geometry to the fragment.
            triangleFragment.getPaths().add(triangle);

            // Specify how the shape is rendered (fill + outline).
            triangleFragment.setAction(PathShapeAction.FILL_AND_STROKE);

            // Set fill and outline colors.
            triangleFragment.setFill(Fill.createSolid(PdfColor.getOrange()));
            triangleFragment.setOutline(
                    Outline.create(Fill.createSolid(PdfColor.getDarkOrange())));

            // Add the shape to the page.
            page.addFragment(triangleFragment);

            // Save the document to a PDF file.
            try (WritableByteChannel writableByteChannel =
                     FileChannel.open(Path.of("result.pdf"),
                         StandardOpenOption.CREATE,
                         StandardOpenOption.WRITE,
                         StandardOpenOption.TRUNCATE_EXISTING)) {

                pdfDocument.save(writableByteChannel);
            }
        }
    }
}

Refer to the following help topic for additional information: Add Shapes to PDF Documents.

Implements

com.devexpress.system.ICloneable

Inherited Members

com.devexpress.system.JavaObject.clone()
com.devexpress.system.JavaObject.equals(java.lang.Object)
com.devexpress.system.JavaObject.hashCode()
com.devexpress.system.JavaObject.initFields()
com.devexpress.system.JavaObject.memberwiseClone()
com.devexpress.system.JavaObject.toString()
java.lang.Object.finalize()
java.lang.Object.getClass()
java.lang.Object.notify()
java.lang.Object.notifyAll()
java.lang.Object.wait()
java.lang.Object.wait(long)
java.lang.Object.wait(long,int)

Inheritance

Object
com.devexpress.system.JavaObject
GraphicsPath

Constructors

GraphicsPath(float startPointX, float startPointY) Constructor

Initializes a new instance of the GraphicsPath class with specified start point coordinates.

Declaration

public GraphicsPath(float startPointX, float startPointY)

Parameters

Name Type Description
startPointX float

The X-coordinate of the start point.

startPointY float

The Y-coordinate of the start point.

GraphicsPath(PointF startPoint) Constructor

Initializes a new instance of the GraphicsPath class with the specified start point.

Declaration

public GraphicsPath(PointF startPoint)

Parameters

Name Type Description
startPoint com.devexpress.system.drawing.PointF

The start point of the graphics path.

Methods

appendBezierSegment(PointF controlPoint1, PointF controlPoint2, PointF endPoint) Method

Appends a cubic Bezier curve segment to the path.

Declaration

public void appendBezierSegment(PointF controlPoint1, PointF controlPoint2, PointF endPoint)

Parameters

Name Type Description
controlPoint1 com.devexpress.system.drawing.PointF

The first control point of the Bezier curve.

controlPoint2 com.devexpress.system.drawing.PointF

The second control point of the Bezier curve.

endPoint com.devexpress.system.drawing.PointF

The end point of the Bezier curve.

Remarks

The following code snippet creates a Bézier curve and adds it to the page:

GraphicsPath bezier = new GraphicsPath(new PointF(210, 375));

bezier.appendBezierSegment(
        new PointF(210, 320),
        new PointF(350, 320),
        new PointF(350, 375));

bezier.appendBezierSegment(
        new PointF(350, 430),
        new PointF(210, 430),
        new PointF(210, 375));

PathFragment bezierFragment = new PathFragment();
bezierFragment.getPaths().add(bezier);
bezierFragment.setAction(PathShapeAction.FILL_AND_STROKE);
bezierFragment.setFill(Fill.createSolid(PdfColor.getOrange()));
bezierFragment.setOutline(Outline.create(Fill.createSolid(PdfColor.getDarkOrange())));

page.addFragment(bezierFragment);

appendLineSegment(PointF endPoint) Method

Appends a line segment to the path.

Declaration

public void appendLineSegment(PointF endPoint)

Parameters

Name Type Description
endPoint com.devexpress.system.drawing.PointF

The end point of the line segment.

clone(DocumentCloneContext context) Method

Creates a copy of the current object.

Declaration

public GraphicsPath clone(DocumentCloneContext context)

Parameters

Name Type Description
context DocumentCloneContext

The document clone context.

Returns

Type Description
GraphicsPath

A copy of the GraphicsPath object.

deepClone() Method

Creates a deep copy of the current object.

Declaration

public GraphicsPath deepClone()

Returns

Type Description
GraphicsPath

A deep copy of the current object.

getClosed() Method

Returns whether the graphics path is closed.

Declaration

public boolean getClosed()

Returns

Type Description
boolean

true if the graphics path is closed; otherwise, false.

getSegments() Method

Returns segments that define the graphics path.

Declaration

public List<GraphicsPathSegment> getSegments()

Returns

Type Description
java.util.List<GraphicsPathSegment>

Segments that define the graphics path.

getStartPoint() Method

Returns the start point of the graphics path.

Declaration

public PointF getStartPoint()

Returns

Type Description
com.devexpress.system.drawing.PointF

The start point of the graphics path.

objectClone() Method

Creates a copy of the current object.

Declaration

public Object objectClone()

Returns

Type Description
Object

A copy of the current object.

setClosed(boolean value) Method

Sets whether the graphics path is closed.

Declaration

public void setClosed(boolean value)

Parameters

Name Type Description
value boolean

true if the graphics path is closed; otherwise, false.