Java 如何剪辑Path2D?

Java 如何剪辑Path2D?,java,java-2d,Java,Java 2d,是否有方法将Path2D剪裁到区域/其他Path2D实例 简单的例子(我正在寻找一些在一般情况下可以工作的东西,其中路径可能包括四边形或立方体,可能是也可能不是单数): 我有一个线段(0,10)->(30,10),我想把它夹在三角形(10,0),(20,20),(20,0)中,理想情况下产生线段(15,10)->(20,10) 我可以使用“new area(Shape);”将Path2D转换为一个区域,然后使用“area.intersect(area)”进行剪辑,但如果路径未闭合,则会返回一个空

是否有方法将Path2D剪裁到区域/其他Path2D实例

简单的例子(我正在寻找一些在一般情况下可以工作的东西,其中路径可能包括四边形或立方体,可能是也可能不是单数):

我有一个线段(0,10)->(30,10),我想把它夹在三角形(10,0),(20,20),(20,0)中,理想情况下产生线段(15,10)->(20,10)

我可以使用“new area(Shape);”将Path2D转换为一个区域,然后使用“area.intersect(area)”进行剪辑,但如果路径未闭合,则会返回一个空区域

我可以使用“Graphics2D.clip(Shape)”剪裁绘图区域,但我希望得到返回的形状(在某些情况下,我希望在实际渲染之前执行进一步的操作)


在浏览了API文档之后,我找不到直接这样做的方法。我遗漏了什么吗?

无法使用区域剪裁路径的原因是Path2D对象的区域为零。路径的宽度未定义。Area类严格用于具有面积的对象

可以剪裁图形,因为已经定义了笔划宽度,用于定义路径区域

因此,如果要剪裁路径,需要使用Stroke.createStrokedShape(shape)从路径创建一个笔划形状

下面是一个例子:

public static void main(String[] args) throws IOException {

    String imgFormat = "PNG";

    Path2D path = new Path2D.Double();
    BasicStroke pathStroke = 
        new BasicStroke( 2 );


    // Create the path to be clipped:       
    int pathPoints[] = { 0, 10, 30, 10 };
    path.moveTo( pathPoints[ 0 ], pathPoints[1] );
    for( int i = 2; i < pathPoints.length; i+=2 ) {
        path.lineTo( pathPoints[ i ], pathPoints[ i+1 ] );
    }
    // Create the shape representing the clip area
    Polygon clipShape = new Polygon();
    int triPoints[] = { 10, 0, 20, 20, 20, 0 };
    for( int i = 0; i < triPoints.length; i+=2 ) {
        clipShape.addPoint( triPoints[i], triPoints[i+1] );
    }
    // Create the path with area using the stroke
    Shape clippedPath = pathStroke.createStrokedShape( path );

    // Apply a scale so the image is easier to see
    int scale = 10;
    AffineTransform at = AffineTransform.getScaleInstance( scale, scale );
    Shape sPath = at.createTransformedShape( path );
    Shape sClip = at.createTransformedShape( clipShape );

    // Create the Area shape that represents the path that is clipped by the clipShape
    Area clipArea = new Area( sClip );
    clipArea.intersect( new Area( at.createTransformedShape( clippedPath ) ) );


    int bbox = 10;
    Rectangle rect = sPath.getBounds();
    rect.add( sClip.getBounds() );
    // Expand the drawing area      
    rect.width += bbox;
    rect.height += bbox;
    rect.x -= bbox/2;
    rect.y -= bbox/2;
    BufferedImage img = new BufferedImage( rect.width, rect.height, BufferedImage.TYPE_INT_ARGB  );
    Graphics2D g2 = img.createGraphics();

    g2.setStroke( pathStroke );
    g2.setColor( Color.black );
    g2.draw( sPath );

    g2.setColor( Color.red );
    g2.draw( sClip );

    g2.setColor( Color.green );
    g2.draw( clipArea );

    g2.finalize();

    File img1 = new File( "/tmp/img1.png" );
    ImageIO.write( img, imgFormat, img1 );
}
publicstaticvoidmain(字符串[]args)引发IOException{
字符串imgFormat=“PNG”;
Path2D path=新的Path2D.Double();
基本行程路径行程=
新基本行程(2);
//创建要剪裁的路径:
int路径点[]={0,10,30,10};
移动到(路径点[0],路径点[1]);
对于(int i=2;i
谢谢您的回复。在某些情况下,首先使用笔划将线转换为区域会很好,尽管我在一些情况下编写了一些自定义几何体代码。