KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Rotate


1 // $Id: Rotate.java,v 1.2 2003/11/03 10:56:14 mike Exp $
2

3 import java.util.*;
4 import java.io.*;
5 import java.awt.Color JavaDoc;
6 import org.faceless.pdf2.*;
7
8 /**
9  * An example showing some of the graphics operations, like save
10  * and restore, bezier curves, line dashing and so on.
11  *
12  */

13 public class Rotate
14 {
15     public static void main(String JavaDoc[] args)
16         throws IOException
17     {
18     // Create a new PDF
19
PDF pdf = new PDF();
20
21     // Create a new page
22
//
23
PDFPage page = pdf.newPage(PDF.PAGESIZE_A4);
24
25     // Create a new style to demonstrate some unusual features
26
// of PDFStyles. Draw the lines 2pt wide in yellow, and make
27
// them dashed (draw five points, skip four points). Also,
28
// make the ends of each dash rounded rather than square.
29
//
30
PDFStyle linestyle = new PDFStyle();
31     linestyle.setLineColor(Color.yellow);
32     linestyle.setLineWeighting(2);
33     linestyle.setLineDash(5,4,0);
34     linestyle.setLineCap(PDFStyle.LINECAP_ROUND);
35
36     // Create a style "bluefill", which has yellow 2pt wide
37
// border and a blue fill.
38
//
39
PDFStyle bluefill = new PDFStyle();
40     bluefill.setFillColor(Color.blue);
41     bluefill.setLineColor(Color.yellow);
42     bluefill.setLineWeighting(2);
43
44     // Create a style "yellowfill" which just fills the path
45
// with yellow
46
//
47
PDFStyle yellowfill = new PDFStyle();
48     yellowfill.setFillColor(Color.yellow);
49
50     // Next, draw a circle with several bezier curves inside, all
51
// rotated around the center.
52
//
53

54     // Save the state first so we can get back to it easily.
55
//
56
page.save();
57     int left = 300;
58     int top = 500;
59
60     // Draw the outer circle, fill it with blue
61
//
62
page.setStyle(bluefill);
63     page.drawCircle(left, top, 160);
64
65     // Draw the "spokes", each one rotated by an extra 10 degrees.
66
// We save and restore the state between each stroke
67
//
68
page.setStyle(linestyle);
69     for (int i=0;i<360;i+=10) {
70         page.save();
71         page.rotate(left, top, i);
72         page.pathMove(left, top+10);
73         page.pathBezier(left-100, top+60, left+100, top+110, left, top+159);
74         page.pathPaint();
75         page.restore();
76     }
77
78     // Draw a circle on the inside
79
//
80
page.setStyle(yellowfill);
81     page.drawCircle(left, top, 10);
82
83     // Go back to our original co-ordinate system.
84
//
85
page.restore();
86
87     //------------------------------------------------------------
88

89     // Done. Write the document to a file
90
//
91
OutputStream fo = new FileOutputStream("Rotate.pdf");
92     pdf.render(fo);
93     fo.close();
94     }
95 }
96
Popular Tags