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.image.BufferedImage;
  22. /**
  23. *
  24. * @see org.apache.tools.ant.taskdefs.optional.image.Image
  25. */
  26. public class Rectangle extends BasicShape implements DrawOperation {
  27. protected int width = 0;
  28. protected int height = 0;
  29. protected int arcwidth = 0;
  30. protected int archeight = 0;
  31. public void setWidth(int w) {
  32. width = w;
  33. }
  34. public void setHeight(int h) {
  35. height = h;
  36. }
  37. public void setArcwidth(int w) {
  38. arcwidth = w;
  39. }
  40. public void setArcheight(int h) {
  41. archeight = h;
  42. }
  43. public PlanarImage executeDrawOperation() {
  44. log("\tCreating Rectangle w=" + width + " h=" + height + " arcw="
  45. + arcwidth + " arch=" + archeight);
  46. BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
  47. Graphics2D graphics = (Graphics2D) bi.getGraphics();
  48. if (!stroke.equals("transparent")) {
  49. BasicStroke b_stroke = new BasicStroke(stroke_width);
  50. graphics.setColor(ColorMapper.getColorByName(stroke));
  51. graphics.setStroke(b_stroke);
  52. if ((arcwidth != 0) || (archeight != 0)) {
  53. graphics.drawRoundRect(0, 0, width, height, arcwidth, archeight);
  54. } else {
  55. graphics.drawRect(0, 0, width, height);
  56. }
  57. }
  58. if (!fill.equals("transparent")) {
  59. graphics.setColor(ColorMapper.getColorByName(fill));
  60. if ((arcwidth != 0) || (archeight != 0)) {
  61. graphics.fillRoundRect(stroke_width, stroke_width,
  62. width - (stroke_width * 2), height - (stroke_width * 2),
  63. arcwidth, archeight);
  64. } else {
  65. graphics.fillRect(stroke_width, stroke_width,
  66. width - (stroke_width * 2), height - (stroke_width * 2));
  67. }
  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
  77. = ((TransformOperation) instr).executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
  78. bi = image.getAsBufferedImage();
  79. }
  80. }
  81. return PlanarImage.wrapRenderedImage(bi);
  82. }
  83. }