Java Mailing List Archive

http://www.junlu.com/

Google
Google
Mailing List
Home
Forum Home
JBoss - Java Application Server
Tomcat - JSP/Servlet container
Struts - A MVC web framework
iText - An open source PDF Java Library
JDOM - JDOM XML Parser
JSP - A mailing list about Java Server Pages specification and reference
J2EE - A mailing list for Java(tm) 2 Platform, Enterprise Edition
J2EE Pattern - An interest list for Sun Java Center J2EE Pattern Catalog
Servlet - A mailing list for discussion about Sun Microsystem's Java Servlet API Technology
Struts & Hibernate
Subjects
JSP editor plugin for eclipse ?
org apache jasper JasperException: Unable to compile class for JSP
Tomcat: Connection reset by peer: socket write error
Cannot retrieve definition for form bean null
Struts Tiles Tutorial (free Struts training)
Where do I download Tomcat 4 0 6?
Data Access Object (DAO) pattern, example DAO 's
Where to download Tomcat v 4 1 24 from?
Tomcat 5 0 16 Requested resource not available
Subject: Servlet : Session invalidate
Oracle Connection Pooling in 3 2 2
Servlet action is currently unavailable
Tomcat/Struts Unicode Encoding/Decoding problems
Subject: Running a Simple JMS Example
Tomcat and webapplication specific java library path
Mapping in workers2 properties
org apache jasper JasperException
problem with html:text bean throwing exception
Cannot find message resources under key org apache struts action
   MESSAGE
Cannot find message resources under key org apache struts action MESSAGE
invalid direct reference problem with solution
Tool for jsp debug Try Sysdeo Eclipse Plugin
Tomcat 5 Cannot load JDBC driver class 'null ' SQL state: null
weblogic ejbc
java properties file
Jboss 3 2 3 Coyote Can 't re
Tomcat 5, Apache2 and mod jk2 integration problem
JBoss example problem new to J2EE
Value attribute of <html:checkbox
url string for connecting jboss to oracle
javax servlet ServletException: BeanUtils populate
5 0 18: Windows XP Pro vs Windows 2000
HTTP Status 404 The requested resource is not available
 
Creating a dynamic Image(using fonts ) with transparent background

Creating a dynamic Image(using fonts ) with transparent background

2005-01-03       - By Randy Paries

 Back
Reply:     1     2  

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 listserv@(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

©2008 junlu.com - Jax Systems, LLC, U.S.A.