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.Arc2D;
  22. import java.awt.image.BufferedImage;
  23. public class Arc extends BasicShape implements DrawOperation {
  24. protected int width = 0;
  25. protected int height = 0;
  26. protected int start = 0;
  27. protected int stop = 0;
  28. protected int type = Arc2D.OPEN;
  29. public void setWidth(int width) {
  30. this.width = width;
  31. }
  32. public void setHeight(int height) {
  33. this.height = height;
  34. }
  35. public void setStart(int start) {
  36. this.start = start;
  37. }
  38. public void setStop(int stop) {
  39. this.stop = stop;
  40. }
  41. /**
  42. * @todo refactor using an EnumeratedAttribute
  43. */
  44. public void setType(String str_type) {
  45. if (str_type.toLowerCase().equals("open")) {
  46. type = Arc2D.OPEN;
  47. } else if (str_type.toLowerCase().equals("pie")) {
  48. type = Arc2D.PIE;
  49. } else if (str_type.toLowerCase().equals("chord")) {
  50. type = Arc2D.CHORD;
  51. }
  52. }
  53. public PlanarImage executeDrawOperation() {
  54. BufferedImage bi = new BufferedImage(width + (stroke_width * 2),
  55. height + (stroke_width * 2), BufferedImage.TYPE_4BYTE_ABGR_PRE);
  56. Graphics2D graphics = (Graphics2D) bi.getGraphics();
  57. if (!stroke.equals("transparent")) {
  58. BasicStroke b_stroke = new BasicStroke(stroke_width);
  59. graphics.setColor(ColorMapper.getColorByName(stroke));
  60. graphics.setStroke(b_stroke);
  61. graphics.draw(new Arc2D.Double(stroke_width, stroke_width, width,
  62. height, start, stop, type));
  63. }
  64. if (!fill.equals("transparent")) {
  65. graphics.setColor(ColorMapper.getColorByName(fill));
  66. graphics.fill(new Arc2D.Double(stroke_width, stroke_width,
  67. width, height, start, stop, type));
  68. }
  69. for (int i = 0; i < instructions.size(); i++) {
  70. ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
  71. if (instr instanceof DrawOperation) {
  72. PlanarImage img = ((DrawOperation) instr).executeDrawOperation();
  73. graphics.drawImage(img.getAsBufferedImage(), null, 0, 0);
  74. } else if (instr instanceof TransformOperation) {
  75. graphics = (Graphics2D) bi.getGraphics();
  76. PlanarImage image = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
  77. bi = image.getAsBufferedImage();
  78. }
  79. }
  80. return PlanarImage.wrapRenderedImage(bi);
  81. }
  82. }