Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Applying clip to a PDFGraphics2D on a translated graphics will translate the graphics twice as much as it should #32

Open
facek opened this issue May 6, 2015 · 0 comments

Comments

@facek
Copy link

facek commented May 6, 2015

Generated images:

java_graphics_test
java_graphics_test_pdf

Reproduction code:

public class javaAndFreeHepPDFGraphicsTest {

public static String javaGraphicsFilePath = "C:\\java_graphics_test.png";
public static String pdfGraphicsFilePath = "C:\\java_graphics_test.pdf";

public static void main(String[] args) throws FileNotFoundException {
testJavaGraphics();
testPdfGraphics();
}

public static void testPdfGraphics() throws FileNotFoundException {

Properties p = new Properties();
p.setProperty("PageSize", "A4");
VectorGraphics g = new PDFGraphics2D(new File(pdfGraphicsFilePath), new Dimension(200, 200));
g.setProperties(p);
g.startExport();

paintToGraphics(g);

g.dispose();
g.endExport();
}

public static void testJavaGraphics() {
BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();

paintToGraphics(g);

g.dispose();


File outputfile = new File(javaGraphicsFilePath);
try {
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

static void paintToGraphics(Graphics2D g) {
g.setClip(0, 0, 200, 200);
g.setColor(Color.BLUE);
g.fillRect(0, 0, 200, 200);
g.translate(50, 50);
Graphics2D g2 = (Graphics2D) g.create(50, 50, 100, 100);
g2.setClip(0, 0, 100, 100);
Shape clip = g2.getClip();
Shape oval = new Ellipse2D.Double(0, 0, 100, 100);
Area areaOval = new Area(oval);
Area clipArea = new Area(clip);
clipArea.subtract(areaOval);
g2.setClip(clipArea);
g2.setColor(Color.RED);
g2.fillRect(0, 0, 100, 100);
g2.dispose();
}

}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant