1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.types.optional.image;
  18. import javax.media.jai.PlanarImage;
  19. import java.awt.Graphics2D;
  20. import java.awt.image.BufferedImage;
  21. /**
  22. *
  23. * @see org.apache.tools.ant.taskdefs.optional.image.Image
  24. */
  25. public class Draw extends TransformOperation {
  26. protected int xloc = 0;
  27. protected int yloc = 0;
  28. public void setXloc(int x) {
  29. xloc = x;
  30. }
  31. public void setYloc(int y) {
  32. yloc = y;
  33. }
  34. public void addRectangle(Rectangle rect) {
  35. instructions.add(rect);
  36. }
  37. public void addText(Text text) {
  38. instructions.add(text);
  39. }
  40. public void addEllipse(Ellipse elip) {
  41. instructions.add(elip);
  42. }
  43. public void addArc(Arc arc) {
  44. instructions.add(arc);
  45. }
  46. public PlanarImage executeTransformOperation(PlanarImage image) {
  47. BufferedImage bi = image.getAsBufferedImage();
  48. Graphics2D graphics = (Graphics2D) bi.getGraphics();
  49. for (int i = 0; i < instructions.size(); i++) {
  50. ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
  51. if (instr instanceof DrawOperation) {
  52. PlanarImage op = ((DrawOperation) instr).executeDrawOperation();
  53. log("\tDrawing to x=" + xloc + " y=" + yloc);
  54. graphics.drawImage(op.getAsBufferedImage(), null, xloc, yloc);
  55. } else if (instr instanceof TransformOperation) {
  56. PlanarImage op
  57. = ((TransformOperation) instr).executeTransformOperation(null);
  58. BufferedImage child = op.getAsBufferedImage();
  59. log("\tDrawing to x=" + xloc + " y=" + yloc);
  60. graphics.drawImage(child, null, xloc, yloc);
  61. PlanarImage.wrapRenderedImage(bi);
  62. }
  63. }
  64. image = PlanarImage.wrapRenderedImage(bi);
  65. return image;
  66. }
  67. }