1/* $Id: pdfclockServlet.java 14574 2005-10-29 16:27:43Z bonefish $
2 *
3 * PDFlib client: pdfclock example in Java
4 */
5
6import java.io.*;
7import java.text.*;		// SimpleDateFormat
8import java.util.*;		// Date
9import javax.servlet.*;
10import com.pdflib.pdflib;
11import com.pdflib.PDFlibException;
12
13public class pdfclockServlet extends GenericServlet
14{
15    public void service (ServletRequest request, ServletResponse response)
16    {
17	pdflib p = null;
18	int tm_hour, tm_min, tm_sec, alpha;
19	float RADIUS = 200.0f;
20	float MARGIN = 20.0f;
21	SimpleDateFormat format;
22	Date now = new Date();
23
24	try{
25	    p = new pdflib();
26	    byte[] buf;
27	    ServletOutputStream out;
28
29	// Generate a PDF in memory; insert a file name to create PDF on disk
30	    if (p.open_file("") == -1) {
31		throw new Exception("Error: " + p.get_errmsg());
32	    }
33
34	    p.set_info("Creator", "pdfclockServlet.java");
35	    p.set_info("Author", "Thomas Merz");
36	    p.set_info("Title", "PDF clock (Java/servlet)");
37
38	    p.begin_page(  (int) (2 * (RADIUS + MARGIN)),
39				    (int) (2 * (RADIUS + MARGIN)));
40
41	    p.translate(RADIUS + MARGIN, RADIUS + MARGIN);
42	    p.setcolor("fillstroke", "rgb", 0.0f, 0.0f, 1.0f, 0.0f);
43	    p.save();
44
45	    // minute strokes
46	    p.setlinewidth(2.0f);
47	    for (alpha = 0; alpha < 360; alpha += 6)
48	    {
49		p.rotate(6.0f);
50		p.moveto(RADIUS, 0.0f);
51		p.lineto(RADIUS-MARGIN/3, 0.0f);
52		p.stroke();
53	    }
54
55	    p.restore();
56	    p.save();
57
58	    // 5 minute strokes
59	    p.setlinewidth(3.0f);
60	    for (alpha = 0; alpha < 360; alpha += 30)
61	    {
62		p.rotate(30.0f);
63		p.moveto(RADIUS, 0.0f);
64		p.lineto(RADIUS-MARGIN, 0.0f);
65		p.stroke();
66	    }
67
68	    format = new SimpleDateFormat("hh");
69	    tm_hour= Integer.parseInt(format.format(now));
70	    format = new SimpleDateFormat("mm");
71	    tm_min = Integer.parseInt(format.format(now));
72	    format = new SimpleDateFormat("ss");
73	    tm_sec = Integer.parseInt(format.format(now));
74
75	    // draw hour hand
76	    p.save();
77	    p.rotate((-((tm_min/60.0f) + tm_hour - 3.0f) * 30.0f));
78	    p.moveto(-RADIUS/10, -RADIUS/20);
79	    p.lineto(RADIUS/2, 0.0f);
80	    p.lineto(-RADIUS/10, RADIUS/20);
81	    p.closepath();
82	    p.fill();
83	    p.restore();
84
85	    // draw minute hand
86	    p.save();
87	    p.rotate((-((tm_sec/60.0f) + tm_min - 15.0f) * 6.0f));
88	    p.moveto(-RADIUS/10, -RADIUS/20);
89	    p.lineto(RADIUS * 0.8f, 0.0f);
90	    p.lineto(-RADIUS/10, RADIUS/20);
91	    p.closepath();
92	    p.fill();
93	    p.restore();
94
95	    // draw second hand
96	    p.setcolor("fillstroke", "rgb", 1.0f, 0.0f, 0.0f, 0.0f);
97	    p.setlinewidth(2);
98	    p.save();
99	    p.rotate(-((tm_sec - 15.0f) * 6.0f));
100	    p.moveto(-RADIUS/5, 0.0f);
101	    p.lineto(RADIUS, 0.0f);
102	    p.stroke();
103	    p.restore();
104
105	    // draw little circle at center
106	    p.circle(0f, 0f, RADIUS/30);
107	    p.fill();
108
109	    p.restore();
110	    p.end_page();
111	    p.close();
112
113	    buf = p.get_buffer();
114
115	    response.setContentType("application/pdf");
116	    response.setContentLength(buf.length);
117	    out = response.getOutputStream();
118	    out.write(buf);
119	    out.close();
120        } catch (PDFlibException e) {
121            System.err.print("PDFlib exception occurred in pdfclock sample:\n");
122            System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
123                            ": " + e.getMessage() + "\n");
124        } catch (Exception e) {
125            System.err.println(e.getMessage());
126        } finally {
127            if (p != null) {
128                p.delete();                     /* delete the PDFlib object */
129            }
130        }
131
132
133    }
134}
135