KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > optional > image > Rectangle


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.types.optional.image;
19
20 import javax.media.jai.PlanarImage;
21 import java.awt.BasicStroke JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24
25 /**
26  *
27  * @see org.apache.tools.ant.taskdefs.optional.image.Image
28  */

29 public class Rectangle extends BasicShape implements DrawOperation {
30     // CheckStyle:VisibilityModifier OFF - bc
31
protected int width = 0;
32     protected int height = 0;
33     protected int arcwidth = 0;
34     protected int archeight = 0;
35     // CheckStyle:VisibilityModifier ON
36

37     /**
38      * Set the width.
39      * @param w the value to use.
40      */

41     public void setWidth(int w) {
42         width = w;
43     }
44
45     /**
46      * Set the height.
47      * @param h the value to use.
48      */

49     public void setHeight(int h) {
50         height = h;
51     }
52
53     /**
54      * Set the arc width.
55      * @param w the value to use.
56      */

57     public void setArcwidth(int w) {
58         arcwidth = w;
59     }
60
61     /**
62      * Set the arc height.
63      * @param h the value to use.
64      */

65     public void setArcheight(int h) {
66         archeight = h;
67     }
68
69     /** {@inheritDoc}. */
70     public PlanarImage executeDrawOperation() {
71         log("\tCreating Rectangle w=" + width + " h=" + height + " arcw="
72             + arcwidth + " arch=" + archeight);
73         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
74
75         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) bi.getGraphics();
76
77         if (!stroke.equals("transparent")) {
78             BasicStroke JavaDoc bStroke = new BasicStroke JavaDoc(stroke_width);
79             graphics.setColor(ColorMapper.getColorByName(stroke));
80             graphics.setStroke(bStroke);
81
82             if ((arcwidth != 0) || (archeight != 0)) {
83                 graphics.drawRoundRect(0, 0, width, height, arcwidth, archeight);
84             } else {
85                 graphics.drawRect(0, 0, width, height);
86             }
87         }
88
89         if (!fill.equals("transparent")) {
90             graphics.setColor(ColorMapper.getColorByName(fill));
91             if ((arcwidth != 0) || (archeight != 0)) {
92                 graphics.fillRoundRect(stroke_width, stroke_width,
93                     width - (stroke_width * 2), height - (stroke_width * 2),
94                     arcwidth, archeight);
95             } else {
96                 graphics.fillRect(stroke_width, stroke_width,
97                     width - (stroke_width * 2), height - (stroke_width * 2));
98             }
99         }
100
101
102         for (int i = 0; i < instructions.size(); i++) {
103             ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
104             if (instr instanceof DrawOperation) {
105                 PlanarImage img = ((DrawOperation) instr).executeDrawOperation();
106                 graphics.drawImage(img.getAsBufferedImage(), null, 0, 0);
107             } else if (instr instanceof TransformOperation) {
108                 graphics = (Graphics2D JavaDoc) bi.getGraphics();
109                 PlanarImage image
110                     = ((TransformOperation) instr)
111                     .executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
112                 bi = image.getAsBufferedImage();
113             }
114         }
115         return PlanarImage.wrapRenderedImage(bi);
116     }
117 }
118
Popular Tags