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")); } }
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )