KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
22 import java.awt.Font JavaDoc;
23 import java.awt.FontMetrics JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.RenderingHints JavaDoc;
26 import java.awt.image.BufferedImage JavaDoc;
27
28 /**
29  *
30  * @see org.apache.tools.ant.taskdefs.optional.image.Image
31  */

32 public class Text extends ImageOperation implements DrawOperation {
33     private static final int DEFAULT_POINT = 10;
34
35     private String JavaDoc strText = "";
36     private String JavaDoc font = "Arial";
37     private int point = DEFAULT_POINT;
38     private boolean bold = false;
39     private boolean italic = false;
40     private String JavaDoc color = "black";
41
42     /**
43      * Set the string to be used as text.
44      * @param str the string to be used.
45      */

46     public void setString(String JavaDoc str) {
47         strText = str;
48     }
49
50     /**
51      * Set the font to be used to draw the text.
52      * @param f the font to be used.
53      */

54     public void setFont(String JavaDoc f) {
55         font = f;
56     }
57
58     /**
59      * Set the number of points to be used.
60      * @param p an integer value as a string.
61      */

62     public void setPoint(String JavaDoc p) {
63         point = Integer.parseInt(p);
64     }
65
66     /**
67      * Set the color of the text.
68      * @param c the color name.
69      */

70     public void setColor(String JavaDoc c) {
71         color = c;
72     }
73
74     /**
75      * @todo is this used?
76      * @param state not used at the moment.
77      */

78     public void setBold(boolean state) {
79         bold = state;
80     }
81
82     /**
83      * @todo is this used?
84      * @param state not used at the moment.
85      */

86     public void setItalic(boolean state) {
87         italic = state;
88     }
89
90     /**
91      * Draw the text.
92      * @return the resultant image.
93      */

94     public PlanarImage executeDrawOperation() {
95         log("\tCreating Text \"" + strText + "\"");
96
97         Color JavaDoc couloir = ColorMapper.getColorByName(color);
98         int width = 1;
99         int height = 1;
100
101         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
102         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) bi.getGraphics();
103         graphics.setRenderingHint(
104             RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
105         graphics.setRenderingHint(
106             RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
107         Font JavaDoc f = new Font JavaDoc(font, Font.PLAIN, point);
108         FontMetrics JavaDoc fmetrics = graphics.getFontMetrics(f);
109         height = fmetrics.getMaxAscent() + fmetrics.getMaxDescent();
110         width = fmetrics.stringWidth(strText);
111
112
113         bi = new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
114         graphics = (Graphics2D JavaDoc) bi.getGraphics();
115
116         graphics.setRenderingHint(
117             RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
118         graphics.setRenderingHint(
119             RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
120
121         graphics.setFont(f);
122         graphics.setColor(couloir);
123         graphics.drawString(strText, 0, height - fmetrics.getMaxDescent());
124         PlanarImage image = PlanarImage.wrapRenderedImage(bi);
125         return image;
126     }
127 }
128
Popular Tags