How an Image can be loaded in a Servlet ?

Answer Posted / sagar

Try to use this code it may help you
public class MessageImage {
/** Creates an Image of a string with an oblique
* shadow behind it. Used by the ShadowedText servlet
* and the ShadowedTextFrame desktop application.
*/
public static Image makeMessageImage(String message,
String fontName,
int fontSize) {
Frame f = new Frame();
// Connect to native screen resource for image creation.
f.addNotify();
// Make sure Java knows about local font names.
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
env.getAvailableFontFamilyNames();
Font font = new Font(fontName, Font.PLAIN, fontSize);
FontMetrics metrics = f.getFontMetrics(font);
int messageWidth = metrics.stringWidth(message);
int baselineX = messageWidth/10;
int width = messageWidth+2*(baselineX + fontSize);
int height = fontSize*7/2;
int baselineY = height*8/10;
Image messageImage = f.createImage(width, height);
Graphics2D g2d =
(Graphics2D)messageImage.getGraphics();
g2d.setFont(font);
g2d.translate(baselineX, baselineY);
g2d.setPaint(Color.lightGray);
AffineTransform origTransform = g2d.getTransform();
g2d.shear(-0.95, 0);
g2d.scale(1, 3);
g2d.drawString(message, 0, 0);
g2d.setTransform(origTransform);
g2d.setPaint(Color.black);
g2d.drawString(message, 0, 0);
return(messageImage);
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of servlet wrapper classes?

892


How to get ip address in jsp login page and how to validate like 127.1.0.1 all should not be greater than 255

3121


What is servlet looping or chaining?

908


What is the difference between the http servlet and generic servlet?

878


Is it possible to have a constructor inside the servlet?

742


How do you communicate in between Applets and Servlets?

812


How is an application exception handling is done using a servlet?

761


How will two or three servlets interact or communicate with each other?

1160


What is servlet attributes and their scope?

787


What is a servlet-to-servlet communcation?

871


What is difference between GenericServlet and HttpServlet?

759


List out difference between a JavaBean from a Servlet?

748


How to handle exceptions thrown by application with another servlet?

820


How the JSP file will be executed on the Server side?

882


What do you mean by cgi in servlet?

814