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

Elucidate servlet attributes and its scope?

781


What is difference between server and servlet?

847


Difference between doget and dopost?

932


What is servlet and how it works?

713


What are important features of Servlet 3?

855


Hi frnd can i any one kindly can post for me portlet,hibernate and spring example application and with flow explantion configuration using Jdeveloper.and related links ar tutorials kindly please send me .its urgent for me .thanks in advance...........else can any one send to kondaiah.goddeti@gmail.com

1958


List out difference between a JavaBean from a Servlet?

776


What do you mean by request dispatcher in servlet? Also explain its methods.

886


What is servlet? Explain

807


What do you mean by filter in servlet?

771


What is the inter-servlet communication?

951


What is servlet in tomcat?

812


What is the difference between encodeRedirectUrl and encodeURL?

866


Can we use the constructor, instead of init(), to initialize servlet?

855


Explain in brief the directory structure of a web application?

834