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.BasicStroke;
  20. import java.awt.Graphics2D;
  21. import java.awt.geom.Ellipse2D;
  22. import java.awt.image.BufferedImage;
  23. /**
  24. *
  25. * @see org.apache.tools.ant.taskdefs.optional.image.Image
  26. */
  27. public class Ellipse extends BasicShape implements DrawOperation {
  28. protected int width = 0;
  29. protected int height = 0;
  30. public void setWidth(int width) {
  31. this.width = width;
  32. }
  33. public void setHeight(int height) {
  34. this.height = height;
  35. }
  36. public PlanarImage executeDrawOperation() {
  37. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
  38. Graphics2D graphics = (Graphics2D) bi.getGraphics();
  39. if (!stroke.equals("transparent")) {
  40. BasicStroke b_stroke = new BasicStroke(stroke_width);
  41. graphics.setColor(ColorMapper.getColorByName(stroke));
  42. graphics.setStroke(b_stroke);
  43. graphics.draw(new Ellipse2D.Double(0, 0, width, height));
  44. }
  45. if (!fill.equals("transparent")) {
  46. graphics.setColor(ColorMapper.getColorByName(fill));
  47. graphics.fill(new Ellipse2D.Double(0, 0, width, height));
  48. }
  49. for (int i = 0; i < instructions.size(); i++) {
  50. ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
  51. if (instr instanceof DrawOperation) {
  52. PlanarImage img = ((DrawOperation) instr).executeDrawOperation();
  53. graphics.drawImage(img.getAsBufferedImage(), null, 0, 0);
  54. } else if (instr instanceof TransformOperation) {
  55. graphics = (Graphics2D) bi.getGraphics();
  56. PlanarImage image = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
  57. bi = image.getAsBufferedImage();
  58. }
  59. }
  60. return PlanarImage.wrapRenderedImage(bi);
  61. }
  62. }