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.

DrawGroupShape.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.sl.draw;
  16. import java.awt.Graphics2D;
  17. import java.awt.geom.AffineTransform;
  18. import java.awt.geom.Rectangle2D;
  19. import org.apache.poi.sl.usermodel.*;
  20. public class DrawGroupShape extends DrawShape {
  21. public DrawGroupShape(GroupShape<?,?> shape) {
  22. super(shape);
  23. }
  24. public void draw(Graphics2D graphics) {
  25. // the coordinate system of this group of shape
  26. Rectangle2D interior = getShape().getInteriorAnchor();
  27. // anchor of this group relative to the parent shape
  28. Rectangle2D exterior = getShape().getAnchor();
  29. AffineTransform tx = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM);
  30. AffineTransform tx0 = new AffineTransform(tx);
  31. double scaleX = interior.getWidth() == 0. ? 1.0 : exterior.getWidth() / interior.getWidth();
  32. double scaleY = interior.getHeight() == 0. ? 1.0 : exterior.getHeight() / interior.getHeight();
  33. tx.translate(exterior.getX(), exterior.getY());
  34. tx.scale(scaleX, scaleY);
  35. tx.translate(-interior.getX(), -interior.getY());
  36. DrawFactory drawFact = DrawFactory.getInstance(graphics);
  37. AffineTransform at2 = graphics.getTransform();
  38. for (Shape<?,?> child : getShape()) {
  39. // remember the initial transform and restore it after we are done with the drawing
  40. AffineTransform at = graphics.getTransform();
  41. graphics.setRenderingHint(Drawable.GSAVE, true);
  42. Drawable draw = drawFact.getDrawable(child);
  43. draw.applyTransform(graphics);
  44. draw.draw(graphics);
  45. // restore the coordinate system
  46. graphics.setTransform(at);
  47. graphics.setRenderingHint(Drawable.GRESTORE, true);
  48. }
  49. graphics.setTransform(at2);
  50. graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, tx0);
  51. }
  52. @Override
  53. protected GroupShape<?,?> getShape() {
  54. return (GroupShape<?,?>)shape;
  55. }
  56. }