KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > examples > OfficeDrawing


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hssf.usermodel.examples;
19
20 import org.apache.poi.hssf.usermodel.*;
21
22 import java.io.IOException JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25
26 /**
27  * Demonstrates how to use the office drawing capabilities of POI.
28  *
29  * @author Glen Stampoultzis (glens at apache.org)
30  */

31 public class OfficeDrawing
32 {
33     public static void main(String JavaDoc[] args)
34         throws IOException JavaDoc
35     {
36         // Create the workbook and sheets.
37
HSSFWorkbook wb = new HSSFWorkbook();
38         HSSFSheet sheet1 = wb.createSheet("new sheet");
39         HSSFSheet sheet2 = wb.createSheet("second sheet");
40         HSSFSheet sheet3 = wb.createSheet("third sheet");
41         HSSFSheet sheet4 = wb.createSheet("fourth sheet");
42
43         // Draw stuff in them
44
drawSheet1( sheet1 );
45         drawSheet2( sheet2 );
46         drawSheet3( sheet3 );
47         drawSheet4( sheet4, wb );
48
49         // Write the file out.
50
FileOutputStream JavaDoc fileOut = new FileOutputStream JavaDoc("workbook.xls");
51         wb.write(fileOut);
52         fileOut.close();
53     }
54
55     private static void drawSheet1( HSSFSheet sheet1 )
56     {
57         // Create a row and size one of the cells reasonably large.
58
HSSFRow row = sheet1.createRow(2);
59         row.setHeight((short) 2800);
60         sheet1.setColumnWidth((short) 2, (short) 9000);
61
62         // Create the drawing patriarch. This is the top level container for
63
// all shapes.
64
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
65
66         // Draw some lines and an oval.
67
drawLinesToCenter( patriarch );
68         drawManyLines( patriarch );
69         drawOval( patriarch );
70         drawPolygon( patriarch );
71
72         // Draw a rectangle.
73
HSSFSimpleShape rect = patriarch.createSimpleShape( new HSSFClientAnchor(100, 100, 900, 200, (short)0, 0, (short)0, 0) );
74         rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
75     }
76
77     private static void drawSheet2( HSSFSheet sheet2 )
78     {
79         // Create a row and size one of the cells reasonably large.
80
HSSFRow row = sheet2.createRow(2);
81         row.setHeightInPoints(240);
82         sheet2.setColumnWidth((short) 2, (short) 9000);
83
84         // Create the drawing patriarch. This is the top level container for
85
// all shapes. This will clear out any existing shapes for that sheet.
86
HSSFPatriarch patriarch = sheet2.createDrawingPatriarch();
87
88         // Draw a grid in one of the cells.
89
drawGrid( patriarch );
90     }
91
92     private static void drawSheet3( HSSFSheet sheet3 )
93     {
94         // Create a row and size one of the cells reasonably large
95
HSSFRow row = sheet3.createRow(2);
96         row.setHeightInPoints(140);
97         sheet3.setColumnWidth((short) 2, (short) 9000);
98
99         // Create the drawing patriarch. This is the top level container for
100
// all shapes. This will clear out any existing shapes for that sheet.
101
HSSFPatriarch patriarch = sheet3.createDrawingPatriarch();
102
103         // Create a shape group.
104
HSSFShapeGroup group = patriarch.createGroup(
105                 new HSSFClientAnchor(0,0,900,200,(short)2,2,(short)2,2));
106
107         // Create a couple of lines in the group.
108
HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3,3,500,500));
109         shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
110         ( (HSSFChildAnchor) shape1.getAnchor() ).setAnchor((short)3,3,500,500);
111         HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor((short)1,200,400,600));
112         shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
113
114     }
115
116     private static void drawSheet4( HSSFSheet sheet4, HSSFWorkbook wb )
117     {
118         // Create the drawing patriarch. This is the top level container for
119
// all shapes. This will clear out any existing shapes for that sheet.
120
HSSFPatriarch patriarch = sheet4.createDrawingPatriarch();
121
122         // Create a couple of textboxes
123
HSSFTextbox textbox1 = patriarch.createTextbox(
124                 new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
125         textbox1.setString(new HSSFRichTextString("This is a test") );
126         HSSFTextbox textbox2 = patriarch.createTextbox(
127                 new HSSFClientAnchor(0,0,900,100,(short)3,3,(short)3,4));
128         textbox2.setString(new HSSFRichTextString("Woo") );
129         textbox2.setFillColor(200,0,0);
130         textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);
131
132         // Create third one with some fancy font styling.
133
HSSFTextbox textbox3 = patriarch.createTextbox(
134                 new HSSFClientAnchor(0,0,900,100,(short)4,4,(short)5,4+1));
135         HSSFFont font = wb.createFont();
136         font.setItalic(true);
137         font.setUnderline(HSSFFont.U_DOUBLE);
138         HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
139         string.applyFont(2,5,font);
140         textbox3.setString(string );
141         textbox3.setFillColor(0x08000030);
142         textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); // no line around the textbox.
143
textbox3.setNoFill(true); // make it transparent
144
}
145
146     private static void drawOval( HSSFPatriarch patriarch )
147     {
148         // Create an oval and style to taste.
149
HSSFClientAnchor a = new HSSFClientAnchor();
150         a.setAnchor((short)2, 2, 20, 20, (short) 2, 2, 190, 80);
151         HSSFSimpleShape s = patriarch.createSimpleShape(a);
152         s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
153         s.setLineStyleColor(10,10,10);
154         s.setFillColor(90,10,200);
155         s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);
156         s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);
157     }
158
159     private static void drawPolygon( HSSFPatriarch patriarch )
160     {
161         // HSSFClientAnchor a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 2, 2, (short) 3, 3 );
162
// HSSFPolygon p = patriarch.createPolygon(a);
163
// p.setPolygonDrawArea(100,100);
164
// p.setPoints( new int[]{30, 90, 50}, new int[]{88, 5, 44} );
165

166
167         HSSFClientAnchor a = new HSSFClientAnchor();
168         a.setAnchor( (short) 2, 2, 0, 0, (short) 3, 3, 1023, 255 );
169         HSSFShapeGroup g = patriarch.createGroup( a );
170         g.setCoordinates(0,0,200,200);
171         HSSFPolygon p1 = g.createPolygon( new HSSFChildAnchor( 0, 0, 200, 200 ) );
172         p1.setPolygonDrawArea( 100, 100 );
173         p1.setPoints( new int[]{0, 90, 50}, new int[]{5, 5, 44} );
174         p1.setFillColor( 0, 255, 0 );
175         HSSFPolygon p2 = g.createPolygon( new HSSFChildAnchor( 20, 20, 200, 200 ) );
176         p2.setPolygonDrawArea( 200, 200 );
177         p2.setPoints( new int[]{120, 20, 150}, new int[]{105, 30, 195} );
178         p2.setFillColor( 255, 0, 0 );
179     }
180
181     private static void drawManyLines( HSSFPatriarch patriarch )
182     {
183         // Draw bunch of lines
184
int x1 = 100;
185         int y1 = 100;
186         int x2 = 800;
187         int y2 = 200;
188         int color = 0;
189         for (int i = 0; i < 10; i++)
190         {
191             HSSFClientAnchor a2 = new HSSFClientAnchor();
192             a2.setAnchor((short) 2, 2, x1, y1, (short) 2, 2, x2, y2);
193             HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
194             shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
195             shape2.setLineStyleColor(color);
196             y1 -= 10;
197             y2 -= 10;
198             color += 30;
199         }
200     }
201
202     private static void drawGrid( HSSFPatriarch patriarch )
203     {
204         // This draws a grid of lines. Since the coordinates space fixed at
205
// 1024 by 256 we use a ratio to get a reasonably square grids.
206

207         double xRatio = 3.22;
208         double yRatio = 0.6711;
209
210         int x1 = 000;
211         int y1 = 000;
212         int x2 = 000;
213         int y2 = 200;
214         for (int i = 0; i < 20; i++)
215         {
216             HSSFClientAnchor a2 = new HSSFClientAnchor();
217             a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ),
218                     (short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio ));
219             HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
220             shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
221
222             x1 += 10;
223             x2 += 10;
224         }
225
226         x1 = 000;
227         y1 = 000;
228         x2 = 200;
229         y2 = 000;
230         for (int i = 0; i < 20; i++)
231         {
232             HSSFClientAnchor a2 = new HSSFClientAnchor();
233             a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ),
234                     (short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio ));
235             HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
236             shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
237
238             y1 += 10;
239             y2 += 10;
240         }
241     }
242
243     private static void drawLinesToCenter( HSSFPatriarch patriarch )
244     {
245         // Draw some lines from and to the corners
246
{
247             HSSFClientAnchor a1 = new HSSFClientAnchor();
248             a1.setAnchor( (short)2, 2, 0, 0, (short) 2, 2, 512, 128);
249             HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
250             shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
251         }
252         {
253             HSSFClientAnchor a1 = new HSSFClientAnchor();
254             a1.setAnchor( (short)2, 2, 512, 128, (short) 2, 2, 1024, 0);
255             HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
256             shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
257         }
258         {
259             HSSFClientAnchor a1 = new HSSFClientAnchor();
260             a1.setAnchor( (short)1, 1, 0, 0, (short) 1, 1, 512, 100);
261             HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
262             shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
263         }
264         {
265             HSSFClientAnchor a1 = new HSSFClientAnchor();
266             a1.setAnchor( (short)1, 1, 512, 100, (short) 1, 1, 1024, 0);
267             HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
268             shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
269         }
270
271     }
272 }
273
Popular Tags