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.

Graphics2DDemo.java 3.2KB

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