KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.*;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * Demonstrates the use of the EscherGraphics2d library.
28  *
29  * @author Glen Stampoultzis (glens at apache.org)
30  */

31 public class OfficeDrawingWithGraphics
32 {
33     public static void main( String JavaDoc[] args ) throws IOException JavaDoc
34     {
35         // Create a workbook with one sheet and size the first three somewhat
36
// larger so we can fit the chemical structure diagram in.
37
HSSFWorkbook wb = new HSSFWorkbook();
38         HSSFSheet sheet = wb.createSheet( "my drawing" );
39         sheet.setColumnWidth((short)1, (short)(256 * 27));
40         HSSFRow row1 = sheet.createRow(0);
41         row1.setHeightInPoints(10 * 15);
42         HSSFRow row2 = sheet.createRow(1);
43         row2.setHeightInPoints(5 * 15);
44         HSSFRow row3 = sheet.createRow(2);
45         row3.setHeightInPoints(10 * 15);
46
47         // Add some cells so we can test that the anchoring works when we
48
// sort them.
49
row1.createCell((short)0).setCellValue("C");
50         row2.createCell((short)0).setCellValue("A");
51         row3.createCell((short)0).setCellValue("B");
52
53         // Create the top level drawing patriarch.
54
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
55
56         HSSFClientAnchor a;
57         HSSFShapeGroup group;
58         EscherGraphics g;
59         EscherGraphics2d g2d;
60         // Anchor entirely within one cell.
61
a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
62         group = patriarch.createGroup( a );
63         group.setCoordinates( 0, 0, 320, 276 );
64         float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
65         g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
66         g2d = new EscherGraphics2d( g );
67         drawStar( g2d );
68
69         a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 1, (short) 1, 1 );
70         group = patriarch.createGroup( a );
71         group.setCoordinates( 0, 0, 640, 276 );
72         verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / (float)Math.abs(group.getY2() - group.getY1());
73 // verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet);
74
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
75         g2d = new EscherGraphics2d( g );
76         drawStar( g2d );
77
78         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("workbook.xls");
79         wb.write(out);
80         out.close();
81
82     }
83
84     private static void drawStar( EscherGraphics2d g2d )
85     {
86         g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
87         for (double i = 0; i < Math.PI; i += 0.1)
88         {
89             g2d.setColor( new Color((int)(i * 5343062d) ) );
90             int x1 = (int) ( Math.cos(i) * 160.0 ) + 160;
91             int y1 = (int) ( Math.sin(i) * 138.0 ) + 138;
92             int x2 = (int) ( -Math.cos(i) * 160.0 ) + 160;
93             int y2 = (int) ( -Math.sin(i) * 138.0 ) + 138;
94             g2d.drawLine(x1,y1,x2,y2);
95         }
96         g2d.setFont(new Font("SansSerif",Font.BOLD | Font.ITALIC, 20));
97         g2d.drawString("EscherGraphics2d",70,100);
98         g2d.setColor(Color.yellow);
99         g2d.fillOval( 160-20,138-20,40,40);
100         g2d.setColor(Color.black);
101         g2d.fillPolygon(new int[] {-10+160,0+160,10+160,0+160}, new int[] {0+138,10+138,0+138,-10+138}, 4);
102         g2d.drawPolygon(new int[] {-160+160,0+160,160+160,0+160}, new int[] {0+138,138+138,0+138,-138+138}, 4);
103     }
104 }
105
Popular Tags