G2DCircleIntersectPDF.java Source code

Java tutorial

Introduction

Here is the source code for G2DCircleIntersectPDF.java

Source

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.DefaultFontMapper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfTemplate;
import com.lowagie.text.pdf.PdfWriter;

public class G2DCircleIntersectPDF {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("G2DCircleIntersectPDF.pdf"));
            document.open();
            DefaultFontMapper mapper = new DefaultFontMapper();
            FontFactory.registerDirectories();
            mapper.insertDirectory("c:\\windows\\fonts");

            int w = 150;
            int h = 150;
            PdfContentByte cb = writer.getDirectContent();
            PdfTemplate tp = cb.createTemplate(w, h);
            Graphics2D g2 = tp.createGraphics(w, h, mapper);
            tp.setWidth(w);
            tp.setHeight(h);
            double ew = w / 2;
            double eh = h / 2;
            Ellipse2D.Double circle, circle1;

            circle = new Ellipse2D.Double(ew - 16, eh - 29, 50.0, 50.0);

            g2.setColor(Color.green);
            g2.fill(circle);

            g2.setColor(Color.red);
            circle1 = new Ellipse2D.Double(ew, eh, 50.0, 50.0);
            g2.fill(circle1);

            Area area1 = new Area(circle);
            Area area2 = new Area(circle1);

            g2.setColor(Color.BLUE);
            area1.intersect(area2);
            g2.fill(area1);

            g2.dispose();
            cb.addTemplate(tp, 50, 400);

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        document.close();
    }

}