graphics/rounded_rectangle
Create a rectangle with rounded corners.
Download Java Code Switch to PHP Code Show Output
/*
* Rounded rectangles:
* Create some rectangle with the corners being rounded
*
* Create two simple rectangles using the linejoin parameter for rounded
* corners. Define a path for a rectangle with corners rounded by a given
* radius. Create three rhombuses using the linejoin and linecap parameters for
* rounded corners.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: none
*/
package com.pdflib.cookbook.pdflib.graphics;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class rounded_rectangle
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "rounded_rectangle.pdf";
String title = "Rounded Rectangle";
pdflib p = null;
int x = 50, y = 100, radius = 50, width = 400, height = 300;
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);
/* Start page 1 */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
p.setlinewidth(15.0);
/* Red rectangle */
p.setcolor("stroke", "rgb", 1.0, 0.0, 0.0, 0.0);
p.rect(100, 700, 100, 100);
p.stroke();
p.fit_textline("Simple rectangle", 250, 750,
"fontname=NotoSerif-Regular fontsize=16");
/* Red rectangle with the "linejoin" parameter set */
p.set_graphics_option("linejoin=1");
p.rect(100, 500, 100, 100);
p.stroke();
p.fit_textline("Simple rectangle with linejoin=1", 250, 550,
"fontname=NotoSerif-Regular fontsize=16");
/* Define a path for a rectangle with corners rounded by a given radius.
* Start from the lower left corner and proceed counterclockwise.
*/
p.moveto(x + radius, y);
/* Start of the arc segment in the lower right corner */
p.lineto(x + width - radius, y);
/* Arc segment in the lower right corner */
p.arc(x + width - radius, y + radius, radius, 270, 360);
/* Start of the arc segment in the upper right corner */
p.lineto(x + width, y + height - radius );
/* Arc segment in the upper right corner */
p.arc(x + width - radius, y + height - radius, radius, 0, 90);
/* Start of the arc segment in the upper left corner */
p.lineto(x + radius, y + height);
/* Arc segment in the upper left corner */
p.arc(x + radius, y + height - radius, radius, 90, 180);
/* Start of the arc segment in the lower left corner */
p.lineto(x , y + radius);
/* Arc segment in the lower left corner */
p.arc(x + radius, y + radius, radius, 180, 270);
p.stroke();
p.fit_textline("Rectangle with corners rounded by a defined radius",
70, 60,
"fontname=NotoSerif-Regular fontsize=16");
/* Reset the linejoin parameter */
p.set_graphics_option("linejoin=0");
p.end_page_ext("");
/* Start page 2 */
p.begin_page_ext(0, 0, "width=a4.width height=a4.height");
p.setlinewidth(15.0);
/* Blue rhombus without the "linejoin" parameter set */
p.setcolor("stroke", "rgb", 0.0, 0.0, 1.0, 0.0);
p.moveto(100, 700); // left corner
p.lineto(200, 800); // upper corner
p.lineto(300, 700); // right corner
p.lineto(200, 600); // lower corner
p.lineto(100, 700); // left corner
p.stroke();
p.fit_textline("Rhombus", 320, 695,
"fontname=NotoSerif-Regular fontsize=16");
/* Blue rhombus with the "linejoin" parameter set */
p.set_graphics_option("linejoin=1");
p.moveto(100, 450); // left corner
p.lineto(200, 550); // upper corner
p.lineto(300, 450); // right corner
p.lineto(200, 350); // lower corner
p.lineto(100, 450); // left corner
p.stroke();
p.fit_textline("Rhombus with linejoin=1", 320, 445,
"fontname=NotoSerif-Regular fontsize=16");
/* blue rhombus with linejoin and linecap parameter */
p.set_graphics_option("linecap=1");
p.moveto(100, 200); // left corner
p.lineto(200, 300); // upper corner
p.lineto(300, 200); // right corner
p.lineto(200, 100); // lower corner
p.lineto(100, 200); // left corner
p.stroke();
p.fit_textline("Rhombus with linejoin=1", 320, 195,
"fontname=NotoSerif-Regular fontsize=16");
p.fit_textline("and linecap=1", 320, 175,
"fontname=NotoSerif-Regular fontsize=16");
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);
}
}
}