1 18 package org.apache.tools.ant.types.optional.image; 19 20 import javax.media.jai.PlanarImage; 21 import java.awt.Color ; 22 import java.awt.Font ; 23 import java.awt.FontMetrics ; 24 import java.awt.Graphics2D ; 25 import java.awt.RenderingHints ; 26 import java.awt.image.BufferedImage ; 27 28 32 public class Text extends ImageOperation implements DrawOperation { 33 private static final int DEFAULT_POINT = 10; 34 35 private String strText = ""; 36 private String font = "Arial"; 37 private int point = DEFAULT_POINT; 38 private boolean bold = false; 39 private boolean italic = false; 40 private String color = "black"; 41 42 46 public void setString(String str) { 47 strText = str; 48 } 49 50 54 public void setFont(String f) { 55 font = f; 56 } 57 58 62 public void setPoint(String p) { 63 point = Integer.parseInt(p); 64 } 65 66 70 public void setColor(String c) { 71 color = c; 72 } 73 74 78 public void setBold(boolean state) { 79 bold = state; 80 } 81 82 86 public void setItalic(boolean state) { 87 italic = state; 88 } 89 90 94 public PlanarImage executeDrawOperation() { 95 log("\tCreating Text \"" + strText + "\""); 96 97 Color couloir = ColorMapper.getColorByName(color); 98 int width = 1; 99 int height = 1; 100 101 BufferedImage bi = new BufferedImage (width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE); 102 Graphics2D graphics = (Graphics2D ) 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 f = new Font (font, Font.PLAIN, point); 108 FontMetrics fmetrics = graphics.getFontMetrics(f); 109 height = fmetrics.getMaxAscent() + fmetrics.getMaxDescent(); 110 width = fmetrics.stringWidth(strText); 111 112 113 bi = new BufferedImage (width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE); 114 graphics = (Graphics2D ) 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 |