Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Generate Image Files 
 

This section illustrates you how to generate image files. For this, we have used the class ImageIO which provides methods for locating ImageReaders and ImageWriters.

 

Generate Image Files

                         

This section illustrates you how to generate image files. For this, we have used the class ImageIO which provides methods for locating ImageReaders and ImageWriters.  

The method createGraphics() creates a Graphics2D and draw into the BufferedImage. To render the specified font on a particular screen, the class FontMetrics is used. The method getFontMetrics() get the font metrics of current font. The method getAscent() provides the font ascent of the font. The method stringWidth(text) returns the total width to show the specified string in the specified font.

The method writes() writes an image using ImageWriter in the file.

Here is the code of GenerateImageType.java

import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

public class GenerateImageType {
  static public void main(String args[]) throws Exception {
      int width = 200, height = 180;
      BufferedImage bufferedImage = new BufferedImage(width, height,
               BufferedImage.TYPE_INT_ARGB);

      Graphics2D g2d = bufferedImage.createGraphics();
    
    Font font = new Font("Arial", Font.BOLD, 24);
      g2d.setFont(font);
      String text = "Welcome!";
      FontMetrics fontMetrics = g2d.getFontMetrics();
      int stringWidth = fontMetrics.stringWidth(text);
      int stringHeight = fontMetrics.getAscent();
      g2d.setPaint(Color.black);
      g2d.drawString(text, (width - stringWidth) / 2, height / 
              stringHeight / 
4);
      
    ImageIO.write(bufferedImage, "gif"new File("C:\\image.GIF"));
      ImageIO.write(bufferedImage, "JPEG"new File("C:\\image.JPG"));
      ImageIO.write(bufferedImage, "PNG"new File("C:\\image.PNG"));
      ImageIO.write(bufferedImage, "BMP"new File("C:\\image.BMP"));
      ImageIO.write(bufferedImage, "TIF"new File("C:\\image.TIF"));
   }
}

After compilation, when you the run the program, the image files are saved in the specified location. The image file contains the following image:

Download Source Code

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.