Hello,
I have a jsp (will eventually move it to a servlet) that generates a
dynamic image
I have been following an article
http://today.java.net/pub/a/today/2004/04/22/images.html (Dynamic Text
section.)
so my simple test looks like:
<table width=100 border=1>
<tr><td><img src="/jsp/test/imageFont.jsp"> </td></tr>
</table>
The problem is i can not figure how how to make the image with a
transparent back ground.
Below is the code in imageFont.jsp
Thanks for any help
/*------------------------------SNIP----------------------------------*/
<%@ page import="java.io.File,
java.awt.*,
java.awt.image.BufferedImage,
java.awt.geom.AffineTransform,
javax.swing.*,
java.io.FileInputStream,
java.awt.font.FontRenderContext,
java.awt.geom.Rectangle2D,
javax.imageio.ImageIO,
java.io.OutputStream
"%>
<%
// configure all of the parameters
String text = "ABC abc XYZ xyz";
if(request.getParameter("text") != null) {
text = request.getParameter("text");
}
String font_file = "babyk.ttf";
if(request.getParameter("font-file") != null) {
font_file = request.getParameter("font-file");
}
font_file = request.getRealPath(font_file);
float size = 20.0f;
if(request.getParameter("size") != null) {
size = Float.parseFloat(request.getParameter("size"));
}
Color background = Color.white;
if(request.getParameter("background") != null) {
background = new Color(Integer.parseInt(
request.getParameter("background"),16));
}
Color color = Color.black;
if(request.getParameter("color") != null) {
color = new Color(Integer.parseInt(
request.getParameter("color"),16));
}
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream
(font_file));
font = font.deriveFont(size);
BufferedImage buffer = new BufferedImage
(1,1,BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = buffer.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
FontRenderContext fc = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(text,fc);
// calculate the size of the text
int width = (int) bounds.getWidth();
int height = (int) bounds.getHeight();
// prepare some output
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
g2 = buffer.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
// actually do the drawing
g2.setColor(background);
g2.fillRect(0,0,width,height);
g2.setColor(color);
g2.drawString(text,0,(int)-bounds.getY());
// set the content type and get the output stream
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
// output the image as png
ImageIO.write(buffer, "png", os);
os.close();
%>
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html