001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.types.optional.image;
019:
020: import javax.media.jai.PlanarImage;
021: import java.awt.BasicStroke;
022: import java.awt.Graphics2D;
023: import java.awt.image.BufferedImage;
024:
025: /**
026: *
027: * @see org.apache.tools.ant.taskdefs.optional.image.Image
028: */
029: public class Rectangle extends BasicShape implements DrawOperation {
030: // CheckStyle:VisibilityModifier OFF - bc
031: protected int width = 0;
032: protected int height = 0;
033: protected int arcwidth = 0;
034: protected int archeight = 0;
035:
036: // CheckStyle:VisibilityModifier ON
037:
038: /**
039: * Set the width.
040: * @param w the value to use.
041: */
042: public void setWidth(int w) {
043: width = w;
044: }
045:
046: /**
047: * Set the height.
048: * @param h the value to use.
049: */
050: public void setHeight(int h) {
051: height = h;
052: }
053:
054: /**
055: * Set the arc width.
056: * @param w the value to use.
057: */
058: public void setArcwidth(int w) {
059: arcwidth = w;
060: }
061:
062: /**
063: * Set the arc height.
064: * @param h the value to use.
065: */
066: public void setArcheight(int h) {
067: archeight = h;
068: }
069:
070: /** {@inheritDoc}. */
071: public PlanarImage executeDrawOperation() {
072: log("\tCreating Rectangle w=" + width + " h=" + height
073: + " arcw=" + arcwidth + " arch=" + archeight);
074: BufferedImage bi = new BufferedImage(width, height,
075: BufferedImage.TYPE_4BYTE_ABGR_PRE);
076:
077: Graphics2D graphics = (Graphics2D) bi.getGraphics();
078:
079: if (!stroke.equals("transparent")) {
080: BasicStroke bStroke = new BasicStroke(stroke_width);
081: graphics.setColor(ColorMapper.getColorByName(stroke));
082: graphics.setStroke(bStroke);
083:
084: if ((arcwidth != 0) || (archeight != 0)) {
085: graphics.drawRoundRect(0, 0, width, height, arcwidth,
086: archeight);
087: } else {
088: graphics.drawRect(0, 0, width, height);
089: }
090: }
091:
092: if (!fill.equals("transparent")) {
093: graphics.setColor(ColorMapper.getColorByName(fill));
094: if ((arcwidth != 0) || (archeight != 0)) {
095: graphics.fillRoundRect(stroke_width, stroke_width,
096: width - (stroke_width * 2), height
097: - (stroke_width * 2), arcwidth,
098: archeight);
099: } else {
100: graphics.fillRect(stroke_width, stroke_width, width
101: - (stroke_width * 2), height
102: - (stroke_width * 2));
103: }
104: }
105:
106: for (int i = 0; i < instructions.size(); i++) {
107: ImageOperation instr = ((ImageOperation) instructions
108: .elementAt(i));
109: if (instr instanceof DrawOperation) {
110: PlanarImage img = ((DrawOperation) instr)
111: .executeDrawOperation();
112: graphics
113: .drawImage(img.getAsBufferedImage(), null, 0, 0);
114: } else if (instr instanceof TransformOperation) {
115: graphics = (Graphics2D) bi.getGraphics();
116: PlanarImage image = ((TransformOperation) instr)
117: .executeTransformOperation(PlanarImage
118: .wrapRenderedImage(bi));
119: bi = image.getAsBufferedImage();
120: }
121: }
122: return PlanarImage.wrapRenderedImage(bi);
123: }
124: }
|