import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
+import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.PathIterator;
* true if the shape is to be drawn filled
*/
private void doDrawing(Shape shape, boolean fill) {
+ Shape clip = getClip();
+ if (clip != null) {
+ if (shape instanceof Line2D
+ && (((Line2D) shape).getX1() == ((Line2D) shape).getX2()
+ || ((Line2D) shape).getY1() == ((Line2D) shape).getY2())) {
+ Rectangle2D bounds = shape.getBounds2D();
+ Rectangle2D newShape = bounds.createIntersection(clip.getBounds2D());
+ ((Line2D) shape).setLine(newShape.getX(),
+ newShape.getY(),
+ newShape.getX() + newShape.getWidth(),
+ newShape.getY() + newShape.getHeight());
+ } else if (shape instanceof Rectangle2D) {
+ Area area = new Area(shape);
+ area.intersect(new Area(clip));
+ shape = area.getBounds2D();
+ } else {
+ Area area = new Area(shape);
+ if (!area.isEmpty() && !area.intersects(clip.getBounds2D())) {
+ return;
+ }
+ }
+ }
if (!fill) {
graphicsObj.newSegment();
}
@Test
public void testDrawGraphicsFillet() throws IOException {
GraphicContext gc = new GraphicContext();
- gc.setClip(new Rectangle(0, 0, 2, 2));
+ gc.setClip(new Rectangle(0, 0, 100, 100));
graphics2D.setGraphicContext(gc);
GraphicsObject go = new GraphicsObject(new Factory(), "test");
graphics2D.setGraphicsObject(go);
int sizeOfGraphicsFillet = 128;
Assert.assertEquals(is.read(), sizeOfGraphicsFillet);
}
+
+ @Test
+ public void testDrawGraphicsFilletClipped() throws IOException {
+ GraphicContext gc = new GraphicContext();
+ gc.setClip(new Rectangle(50, 50, 100, 100));
+ graphics2D.setGraphicContext(gc);
+ GraphicsObject go = new GraphicsObject(new Factory(), "test");
+ graphics2D.setGraphicsObject(go);
+ graphics2D.draw(new Ellipse2D.Double(0, 0, 50, 50));
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ go.writeToStream(bos);
+ Assert.assertEquals(bos.size(), 17 + 17);
+ }
}