KickJava   Java API By Example, From Geeks To Geeks.

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


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.geom.Arc2D JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25
26 /**
27  * Draw an arc.
28  */

29 public class Arc extends BasicShape implements DrawOperation {
30     // CheckStyle:VisibilityModifier OFF - bc
31
protected int width = 0;
32     protected int height = 0;
33     protected int start = 0;
34     protected int stop = 0;
35     protected int type = Arc2D.OPEN;
36     // CheckStyle:VisibilityModifier ON
37

38     /**
39      * Set the width.
40      * @param width the width of the arc.
41      */

42     public void setWidth(int width) {
43         this.width = width;
44     }
45
46     /**
47      * Set the height.
48      * @param height the height of the arc.
49      */

50     public void setHeight(int height) {
51         this.height = height;
52     }
53
54     /**
55      * Set the start of the arc.
56      * @param start the start of the arc.
57      */

58     public void setStart(int start) {
59         this.start = start;
60     }
61
62     /**
63      * Set the stop of the arc.
64      * @param stop the stop of the arc.
65      */

66     public void setStop(int stop) {
67         this.stop = stop;
68     }
69
70     /**
71      * Set the type of arc.
72      * @param strType the type to use - open, pie or chord.
73      * @todo refactor using an EnumeratedAttribute
74      */

75     public void setType(String JavaDoc strType) {
76         if (strType.toLowerCase().equals("open")) {
77             type = Arc2D.OPEN;
78         } else if (strType.toLowerCase().equals("pie")) {
79             type = Arc2D.PIE;
80         } else if (strType.toLowerCase().equals("chord")) {
81             type = Arc2D.CHORD;
82         }
83     }
84
85     /** {@inheritDoc}. */
86     public PlanarImage executeDrawOperation() {
87         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(width + (stroke_width * 2),
88             height + (stroke_width * 2), BufferedImage.TYPE_4BYTE_ABGR_PRE);
89
90         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) bi.getGraphics();
91
92         if (!stroke.equals("transparent")) {
93             BasicStroke JavaDoc bStroke = new BasicStroke JavaDoc(stroke_width);
94             graphics.setColor(ColorMapper.getColorByName(stroke));
95             graphics.setStroke(bStroke);
96             graphics.draw(new Arc2D.Double JavaDoc(stroke_width, stroke_width, width,
97                 height, start, stop, type));
98         }
99
100         if (!fill.equals("transparent")) {
101             graphics.setColor(ColorMapper.getColorByName(fill));
102             graphics.fill(new Arc2D.Double JavaDoc(stroke_width, stroke_width,
103                 width, height, start, stop, type));
104         }
105
106
107         for (int i = 0; i < instructions.size(); i++) {
108             ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
109             if (instr instanceof DrawOperation) {
110                 PlanarImage img = ((DrawOperation) instr).executeDrawOperation();
111                 graphics.drawImage(img.getAsBufferedImage(), null, 0, 0);
112             } else if (instr instanceof TransformOperation) {
113                 graphics = (Graphics2D JavaDoc) bi.getGraphics();
114                 PlanarImage image = ((TransformOperation) instr)
115                     .executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
116                 bi = image.getAsBufferedImage();
117             }
118         }
119         return PlanarImage.wrapRenderedImage(bi);
120     }
121 }
122
Popular Tags