您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Graphics2DDemo.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.hslf.examples;
  16. import org.apache.poi.hslf.usermodel.*;
  17. import org.apache.poi.hslf.model.*;
  18. import java.awt.*;
  19. import java.io.FileOutputStream;
  20. /**
  21. * Demonstrates how to draw into a slide using the HSLF Graphics2D driver.
  22. *
  23. * @author Yegor Kozlov
  24. */
  25. public final class Graphics2DDemo {
  26. /**
  27. * A simple bar chart demo
  28. */
  29. public static void main(String[] args) throws Exception {
  30. HSLFSlideShow ppt = new HSLFSlideShow();
  31. //bar chart data. The first value is the bar color, the second is the width
  32. Object[] def = new Object[]{
  33. Color.yellow, new Integer(40),
  34. Color.green, new Integer(60),
  35. Color.gray, new Integer(30),
  36. Color.red, new Integer(80),
  37. };
  38. HSLFSlide slide = ppt.createSlide();
  39. HSLFGroupShape group = new HSLFGroupShape();
  40. //define position of the drawing in the slide
  41. Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
  42. group.setAnchor(bounds);
  43. group.setInteriorAnchor(new java.awt.Rectangle(0, 0, 100, 100));
  44. slide.addShape(group);
  45. Graphics2D graphics = new PPGraphics2D(group);
  46. //draw a simple bar graph
  47. int x = 10, y = 10;
  48. graphics.setFont(new Font("Arial", Font.BOLD, 10));
  49. for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
  50. graphics.setColor(Color.black);
  51. int width = ((Integer)def[i+1]).intValue();
  52. graphics.drawString("Q" + idx, x-5, y+10);
  53. graphics.drawString(width + "%", x + width+3, y + 10);
  54. graphics.setColor((Color)def[i]);
  55. graphics.fill(new Rectangle(x, y, width, 10));
  56. y += 15;
  57. }
  58. graphics.setColor(Color.black);
  59. graphics.setFont(new Font("Arial", Font.BOLD, 14));
  60. graphics.draw(group.getInteriorAnchor());
  61. graphics.drawString("Performance", x + 30, y + 10);
  62. FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
  63. ppt.write(out);
  64. out.close();
  65. }
  66. }