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

What are setSecure() and getSecure() methods in Cookies?

1 Answers  


I have a requirement Here we have a ResultSet object that will contain 50 records i need to print those recors in to a webpage(i.e; view according to MVC architectures that mybe servlet or jsp) . Here i need to print the records 10 per page that is 1 to 10 in page one and 11 to 20 in page two like remaining will be appeared in other pages we need to display those page numbers whenever we click on that page number we will go to that page and display 10 records like we will display 5 pages it is like this << 1 2 3 4 5 next >>

0 Answers   ProKarma,


Why is init() method is used in servlets?

0 Answers  


can i override service method

3 Answers   Intelligroup,


Given the request path below, which are context path, servlet path and path info?

0 Answers  






List some life cycle methods of a servlet.

0 Answers  


What is the importance of init() method in Servlet ?

0 Answers   BirlaSoft,


what is meant by Transaction Isolation Levels?

2 Answers   IBM,


How threads are implemented in servlets?

3 Answers  


What is the use of servletconfig interface?

0 Answers  


How do we call one servlet from another servlet?

0 Answers  


how to make the IP address to .com

1 Answers   iGate,


Categories