1 2 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 ; 24 import java.io.IOException ; 25 26 31 public class OfficeDrawingWithGraphics 32 { 33 public static void main( String [] args ) throws IOException 34 { 35 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 row1.createCell((short)0).setCellValue("C"); 50 row2.createCell((short)0).setCellValue("A"); 51 row3.createCell((short)0).setCellValue("B"); 52 53 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); 55 56 HSSFClientAnchor a; 57 HSSFShapeGroup group; 58 EscherGraphics g; 59 EscherGraphics2d g2d; 60 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 g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel ); 75 g2d = new EscherGraphics2d( g ); 76 drawStar( g2d ); 77 78 FileOutputStream out = new FileOutputStream ("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 |