How an Image can be loaded in a Servlet ?



How an Image can be loaded in a Servlet ?..

Answer / 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

More Servlets Interview Questions

When servlet is loaded?

0 Answers  


what are the way a client can be tracked?

3 Answers   ABC, IBM,


What is the difference between sendredirect() and forward() in a servlet?

0 Answers  


What is a servlet-to-servlet communcation?

0 Answers  


Why do we have servlet filters?

0 Answers  


Can a servlet be called by passing its name as a parameter in the URL?

1 Answers   iFlex,


can i call init() method in destroy() method of servlset. ?

5 Answers   IBM,


What is httpservlet and how it is different from genericservlet?

0 Answers  


what is the difference between do get/dopost

9 Answers  


What is the use of java servlet api?

0 Answers  


What is SingleThreadModel interface?

0 Answers  


What is the purpose of requestdispatcher interface?

0 Answers  


Categories