Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with creating dynamic text on a jsp

Status
Not open for further replies.

paries

Programmer
Mar 8, 2003
4
US
Hello,
I am trying to create dynamic text on a JSP
I am following the example
(Dynamic Text section.)
I have that part working just fine. The problem i am having is how to create the text image with a transparent background

Thanks
 
there's libraries that will create an Image out of a String.
You can then save that Image as a gif or png with the desired transparancy settings using appropriate libraries for that.

I'll leave the searching for those libraries to you (I know they exist but have forgotten where).
 
only netscape can see this png with color red as the transparent color, I don't know why
Code:
<%@ page import="java.io.*" %>
<%@ page import="java.awt.*"
%><%@ page import="java.awt.geom.*"
%><%@ page import="java.awt.image.*"
%><%@ page import="java.awt.font.*"
%><%@ page import="java.io.*"
%><%@ page import="org.joshy.*"
%><%@ page import="javax.swing.*"
%><%@ page import="javax.imageio.*"%>
<%@ page import="com.sun.media.jai.codec.*"

%>
<%
    try {
    
        // configure all of the parameters
        String text = "ABC abc XYZ xyz";
        if(request.getParameter("text") != null) {
            text = request.getParameter("text");
        }
        String font_file = "dungeon.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_RGB);
        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_RGB);
        g2 = buffer.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
        g2.setFont(font);
        
        // actually do the drawing
        g2.setColor(new Color(255,0,0));
        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 sos = response.getOutputStream();

        // output the image as png
	PNGEncodeParam param = new PNGEncodeParam.RGB();
	//param.setBitDepth(8);
((PNGEncodeParam.RGB)param).setTransparentRGB(new int[]{255,0,0});
             ImageEncoder enc = ImageCodec.createImageEncoder("PNG",sos,param);
;
             enc.encode(buffer);
             sos.close();
    } catch (Exception ex) {
        System.out.println(ex);
    }
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top