001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel.examples;
019:
020: import org.apache.poi.hssf.usermodel.*;
021:
022: import java.awt.*;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025:
026: /**
027: * Demonstrates the use of the EscherGraphics2d library.
028: *
029: * @author Glen Stampoultzis (glens at apache.org)
030: */
031: public class OfficeDrawingWithGraphics {
032: public static void main(String[] args) throws IOException {
033: // Create a workbook with one sheet and size the first three somewhat
034: // larger so we can fit the chemical structure diagram in.
035: HSSFWorkbook wb = new HSSFWorkbook();
036: HSSFSheet sheet = wb.createSheet("my drawing");
037: sheet.setColumnWidth((short) 1, (short) (256 * 27));
038: HSSFRow row1 = sheet.createRow(0);
039: row1.setHeightInPoints(10 * 15);
040: HSSFRow row2 = sheet.createRow(1);
041: row2.setHeightInPoints(5 * 15);
042: HSSFRow row3 = sheet.createRow(2);
043: row3.setHeightInPoints(10 * 15);
044:
045: // Add some cells so we can test that the anchoring works when we
046: // sort them.
047: row1.createCell((short) 0).setCellValue("C");
048: row2.createCell((short) 0).setCellValue("A");
049: row3.createCell((short) 0).setCellValue("B");
050:
051: // Create the top level drawing patriarch.
052: HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
053:
054: HSSFClientAnchor a;
055: HSSFShapeGroup group;
056: EscherGraphics g;
057: EscherGraphics2d g2d;
058: // Anchor entirely within one cell.
059: a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, 0,
060: (short) 1, 0);
061: group = patriarch.createGroup(a);
062: group.setCoordinates(0, 0, 320, 276);
063: float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet)
064: / (float) Math.abs(group.getY2() - group.getY1());
065: g = new EscherGraphics(group, wb, Color.black,
066: verticalPointsPerPixel);
067: g2d = new EscherGraphics2d(g);
068: drawStar(g2d);
069:
070: a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, 1,
071: (short) 1, 1);
072: group = patriarch.createGroup(a);
073: group.setCoordinates(0, 0, 640, 276);
074: verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet)
075: / (float) Math.abs(group.getY2() - group.getY1());
076: // verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet);
077: g = new EscherGraphics(group, wb, Color.black,
078: verticalPointsPerPixel);
079: g2d = new EscherGraphics2d(g);
080: drawStar(g2d);
081:
082: FileOutputStream out = new FileOutputStream("workbook.xls");
083: wb.write(out);
084: out.close();
085:
086: }
087:
088: private static void drawStar(EscherGraphics2d g2d) {
089: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
090: RenderingHints.VALUE_ANTIALIAS_ON);
091: for (double i = 0; i < Math.PI; i += 0.1) {
092: g2d.setColor(new Color((int) (i * 5343062d)));
093: int x1 = (int) (Math.cos(i) * 160.0) + 160;
094: int y1 = (int) (Math.sin(i) * 138.0) + 138;
095: int x2 = (int) (-Math.cos(i) * 160.0) + 160;
096: int y2 = (int) (-Math.sin(i) * 138.0) + 138;
097: g2d.setStroke(new BasicStroke(2));
098: g2d.drawLine(x1, y1, x2, y2);
099: }
100: g2d.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 20));
101: g2d.drawString("EscherGraphics2d", 70, 100);
102: g2d.setColor(Color.yellow);
103: g2d.fillOval(160 - 20, 138 - 20, 40, 40);
104: g2d.setColor(Color.black);
105: g2d.fillPolygon(new int[] { -10 + 160, 0 + 160, 10 + 160,
106: 0 + 160 }, new int[] { 0 + 138, 10 + 138, 0 + 138,
107: -10 + 138 }, 4);
108: g2d.drawPolygon(new int[] { -160 + 160, 0 + 160, 160 + 160,
109: 0 + 160 }, new int[] { 0 + 138, 138 + 138, 0 + 138,
110: -138 + 138 }, 4);
111: }
112: }
|