Tue Nov 11, 2008 8:00 pm
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 / 2 +
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"));
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.