Create dynamic image using Java

This post is the first lesson in java 2D series intended to teach you how to use Java  2D to create dynamic images.


A Brief Idea

The Java 2D™ API provides two-dimensional graphics, text, and imaging capabilities for Java™ programs through extensions to the Abstract Windowing Toolkit (AWT). Read More

The Java 2D API provides following capabilities:
  • A uniform rendering model for display devices and printers
  • A wide range of geometric primitives, such as curves, rectangles, and ellipses, as well as a mechanism for rendering virtually any geometric shape
  • Mechanisms for performing hit detection on shapes, text, and images
  • A  model that provides control over how overlapping objects are rendered
  • Enhanced color support that facilitates color management
  • Support for printing complex documents
  • Control of the quality of the rendering through the use of rendering hints

Here I am going to write web based application which will create an image and render it to the web browser.
The dynamic image will be created by buffer image in the memory and then will be displayed on web browser.The buffer image doesn't have limitation of the format of the image, it may be stored in any format. 
Here We Go

This is my dynamic image creator class namely

package com.sarf.dynamic.image;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class DynamicImageCreator {
      public BufferedImage createImage()
            int width = 160;
            int height = 100;
            int imageType = BufferedImage.TYPE_4BYTE_ABGR;
            BufferedImage imageBuffer = new BufferedImage(width,height,imageType);
            Graphics2D graphics = imageBuffer.createGraphics();
            // true means to repeat pattern
            GradientPaint gradientBG = new GradientPaint(0, 0,, 175,
                        175, Color.yellow,true);     
            graphics.fillRect(0, 0, imageBuffer.getWidth(), imageBuffer.getHeight());
            graphics.setFont(new Font(Font.SANS_SERIF,Font.BOLD,20));
            GradientPaint gradientText = new GradientPaint(0, 0, Color.BLUE,
                        100, 20, Color.CYAN,true);   
            graphics.drawString("Sarf 2D",55,40);
            // File file = new File("D:\\img.png");
            // ImageIO.write(imageBuffer, "png", file);
            return imageBuffer;

You can use this class to create an image and store it to disk. You have to use below lines for writing image into file.
   File file = new File("D:\\img.png");
   ImageIO.write(imageBuffer, "png", file);

Now I am going to give you a servlet which will use above class to create dynamic image and send the image to web browser.

package com.sarf.dynamic.image.controller;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sarf.dynamic.image.DynamicImageCreator;

public class DynamicImageController extends HttpServlet {
      private static final long serialVersionUID = 1L;

    public DynamicImageController() {

      protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            DynamicImageCreator obj = new DynamicImageCreator();
            BufferedImage image = obj.createImage();
            OutputStream out = response.getOutputStream();

      protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {


Here is my web.xml 

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"

Here is my index.html content.

            <img id="img1"
                  src = "./DynamicImageController"
                  width="160" height="100" alt="Sarf Here" >


Comments are closed.