graphics/fill_rules
Define some overlapping vector graphics and fill them using various methods.
Download Java Code Switch to PHP Code Show Output
/*
* Fill Rules:
* Define some overlapping vector graphics and fill them using
* various methods
*
* Use the default "nonzero winding number" rule to fill overlapping objects and
* create a ring, for example.
* Use the "evenodd" fill rule to make overlapping objects being
* filled alternately.
*
* Required software: PDFlib/PDFlib+PDI/PPS 9
* Required data: none
*/
package com.pdflib.cookbook.pdflib.graphics;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class fill_rules {
public static void main(String argv[]) {
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "fill_rules.pdf";
String title = "Fill Rules";
pdflib p = null;
int font;
int exitcode = 0;
try {
p = new pdflib();
p.set_option("searchpath={" + searchpath + "}");
/* This means we must check return values of load_font() etc. */
p.set_option("errorpolicy=return");
if (p.begin_document(outfile, "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title);
font = p.load_font("NotoSerif-Regular", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
/*
* --------------------------------------------------------------
* Case I: The default "nonzero winding number" rule will be
* used. All defined objects will be filled with one p.fill() call.
* In this case (without any coordinate transformation being
* performed), overlapping objects which are drawn counterclockwise
* will be filled with the current fill color while all objects
* drawn in clockwise direction won't be filled. Note that
* p.circle() and p.rect() functions define objects which will be
* drawn counterclockwise.
* --------------------------------------------------------------
*/
/* Start Page 1 */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
/* Set the color to purple */
p.setcolor("fill", "rgb", 1.0, 0.0, 0.5, 0.0);
/*
* Define a big arc segment of 360 degrees in counterclockwise
* direction. Move to the starting point of the arc segment first
*/
p.moveto(450, 600);
p.arc(300, 600, 150, 0, 360);
/*
* Define a small arc segment of 360 degrees in counterclockwise
* direction. Move to the starting point of the arc segment first
*/
p.moveto(400, 600);
p.arc(300, 600, 100, 0, 360);
/*
* Define a big arc segment of 360 degrees in clockwise direction.
* Move to the starting point of the arc segment first
*/
p.moveto(450, 200);
p.arcn(300, 200, 150, 360, 0);
/*
* Define a small arc segment of 360 degrees in counterclockwise
* direction. Move to the starting point of the arc segment first
*/
p.moveto(400, 200);
p.arc(300, 200, 100, 0, 360);
p.fill_stroke();
/* Set the color to black */
p.setcolor("fill", "gray", 0, 0.0, 0, 0.0);
p.set_text_option("font=" + font + " fontsize=14");
p.fit_textline("With the default \"nonzero winding number\" rule:",
20, 790, "");
p.fit_textline(
"Stroke a big and a small arc segment counterclockwise "
+ "and fill them with purple", 20, 770, "");
p.fit_textline("With the default \"nonzero winding number\" rule:",
20, 400, "");
p.fit_textline("Stroke a big arc segment clockwise and", 20, 380,
"");
p.fit_textline(
"a small arc segment counterclockwise and fill them with " +
"purple", 20, 360, "");
p.end_page_ext("");
/*
* -----------------------------------------------------------------
* Case II: The fill rule of "evenodd" will be used. All defined
* objects will be filled with one p.fill() call. In this case,
* overlapping objects will be drawn according to the "evenodd" rule
* and filled alternately.
* -----------------------------------------------------------------
*/
/* Start Page 2 */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
p.set_graphics_option("fillrule=evenodd");
/* Set the color to purple */
p.setcolor("fill", "rgb", 1.0, 0.0, 0.5, 0.0);
/* Define a rectangle */
p.rect(100, 100, 400, 300);
/* Define a big circle */
p.circle(300, 500, 200);
/* Define an arc segment */
p.moveto(300, 750);
p.lineto(300, 500);
p.arc(300, 500, 250, 0, 90);
/* Define a small circle */
p.circle(300, 600, 50);
p.fill_stroke();
/* Set the color to black */
p.setcolor("fill", "gray", 0, 0.0, 0, 0.0);
p.set_text_option("font=" + font + " fontsize=14");
p.fit_textline(
"With the \"evenodd\" fill rule: Draw a rectangle, a big " +
"circle,", 20, 810, "");
p.fit_textline(
"an arc segment, and a small circle and fill them with purple",
20, 790, "");
p.end_page_ext("");
p.end_document("");
}
catch (PDFlibException e) {
System.err.println("PDFlib exception occurred:");
System.err.println("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg());
exitcode = 1;
}
catch (Exception e) {
System.err.println(e);
exitcode = 1;
}
finally {
if (p != null) {
p.delete();
}
System.exit(exitcode);
}
}
}