The reason you cannot copy the path using the region is because the Path2D object has a region of zero. The width of the path is undefined. The Area class is intended for use with objects with an area.
You can crop the drawing because you have determined the stroke width, which is used to define the path area.
So, if you want to copy the path, you need to create the laid out shape from the path using Stroke.createStrokedShape (Shape)
Here is an example:
public static void main(String[] args) throws IOException {
String imgFormat = "PNG";
Path2D path = new Path2D.Double();
BasicStroke pathStroke =
new BasicStroke( 2 );
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 ] );
}
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] );
}
Shape clippedPath = pathStroke.createStrokedShape( path );
int scale = 10;
AffineTransform at = AffineTransform.getScaleInstance( scale, scale );
Shape sPath = at.createTransformedShape( path );
Shape sClip = at.createTransformedShape( clipShape );
Area clipArea = new Area( sClip );
clipArea.intersect( new Area( at.createTransformedShape( clippedPath ) ) );
int bbox = 10;
Rectangle rect = sPath.getBounds();
rect.add( sClip.getBounds() );
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 );
}
James source
share