You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OfficeDrawingWithGraphics.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.hssf.usermodel;
  16. import java.awt.BasicStroke;
  17. import java.awt.Color;
  18. import java.awt.Font;
  19. import java.awt.RenderingHints;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import org.apache.poi.hssf.usermodel.EscherGraphics;
  23. import org.apache.poi.hssf.usermodel.EscherGraphics2d;
  24. import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
  25. import org.apache.poi.hssf.usermodel.HSSFPatriarch;
  26. import org.apache.poi.hssf.usermodel.HSSFRow;
  27. import org.apache.poi.hssf.usermodel.HSSFShapeGroup;
  28. import org.apache.poi.hssf.usermodel.HSSFSheet;
  29. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  30. /**
  31. * Demonstrates the use of the EscherGraphics2d library.
  32. *
  33. * @author Glen Stampoultzis (glens at apache.org)
  34. */
  35. public class OfficeDrawingWithGraphics {
  36. public static void main( String[] args ) throws IOException {
  37. // Create a workbook with one sheet and size the first three somewhat
  38. // larger so we can fit the chemical structure diagram in.
  39. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  40. HSSFSheet sheet = wb.createSheet("my drawing");
  41. sheet.setColumnWidth(1, 256 * 27);
  42. HSSFRow row1 = sheet.createRow(0);
  43. row1.setHeightInPoints(10 * 15f);
  44. HSSFRow row2 = sheet.createRow(1);
  45. row2.setHeightInPoints(5 * 15f);
  46. HSSFRow row3 = sheet.createRow(2);
  47. row3.setHeightInPoints(10 * 15f);
  48. // Add some cells so we can test that the anchoring works when we
  49. // sort them.
  50. row1.createCell(0).setCellValue("C");
  51. row2.createCell(0).setCellValue("A");
  52. row3.createCell(0).setCellValue("B");
  53. // Create the top level drawing patriarch.
  54. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
  55. HSSFClientAnchor a;
  56. HSSFShapeGroup group;
  57. EscherGraphics g;
  58. EscherGraphics2d g2d;
  59. // Anchor entirely within one cell.
  60. a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, 0, (short) 1, 0);
  61. group = patriarch.createGroup(a);
  62. group.setCoordinates(0, 0, 320, 276);
  63. float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / Math.abs(group.getY2() - group.getY1());
  64. g = new EscherGraphics(group, wb, Color.black, verticalPointsPerPixel);
  65. g2d = new EscherGraphics2d(g);
  66. drawStar(g2d);
  67. a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 1, 1, (short) 1, 1);
  68. group = patriarch.createGroup(a);
  69. group.setCoordinates(0, 0, 640, 276);
  70. verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / Math.abs(group.getY2() - group.getY1());
  71. // verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet);
  72. g = new EscherGraphics(group, wb, Color.black, verticalPointsPerPixel);
  73. g2d = new EscherGraphics2d(g);
  74. drawStar(g2d);
  75. try (FileOutputStream out = new FileOutputStream("workbook.xls")) {
  76. wb.write(out);
  77. }
  78. }
  79. }
  80. private static void drawStar( EscherGraphics2d g2d )
  81. {
  82. g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
  83. for (double i = 0; i < Math.PI; i += 0.1)
  84. {
  85. g2d.setColor( new Color((int)(i * 5343062d) ) );
  86. int x1 = (int) ( Math.cos(i) * 160.0 ) + 160;
  87. int y1 = (int) ( Math.sin(i) * 138.0 ) + 138;
  88. int x2 = (int) ( -Math.cos(i) * 160.0 ) + 160;
  89. int y2 = (int) ( -Math.sin(i) * 138.0 ) + 138;
  90. g2d.setStroke(new BasicStroke(2));
  91. g2d.drawLine(x1,y1,x2,y2);
  92. }
  93. g2d.setFont(new Font("SansSerif",Font.BOLD | Font.ITALIC, 20));
  94. g2d.drawString("EscherGraphics2d",70,100);
  95. g2d.setColor(Color.yellow);
  96. g2d.fillOval( 160-20,138-20,40,40);
  97. g2d.setColor(Color.black);
  98. g2d.fillPolygon(new int[] {-10+160,0+160,10+160,0+160}, new int[] {0+138,10+138,0+138,-10+138}, 4);
  99. g2d.drawPolygon(new int[] {-160+160,0+160,160+160,0+160}, new int[] {0+138,138+138,0+138,-138+138}, 4);
  100. }
  101. }