Home » Desktop Java » imageio » Create image file from graphics object

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Create image file from graphics object

With this tutorial we shall show you how to create an image file from graphics object. This is particularly useful when you want to create your own images out of custom made graphics.

Creating an image file from graphics object requires that you:

  • Create a new BufferedImage.
  • Create a Graphics2D using createGraphics.
  • Create a new File("myimage.png").
  • Use ImageIO.write(bufferedImage, "jpg", file) to create the image.

 
Let’s see the code:

package com.javacodegeeks.snippets.desktop;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class CreateImageFileFromGraphicsObject {

	public static void main(String[] args) throws IOException {

		int width = 250;
	    int height = 250;

	    // Constructs a BufferedImage of one of the predefined image types.
	    BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

	    // Create a graphics which can be used to draw into the buffered image
	    Graphics2D g2d = bufferedImage.createGraphics();

	    // fill all the image with white
	    g2d.setColor(Color.white);
	    g2d.fillRect(0, 0, width, height);

	    // create a circle with black
	    g2d.setColor(Color.black);
	    g2d.fillOval(0, 0, width, height);

	    // create a string with yellow
	    g2d.setColor(Color.yellow);
	    g2d.drawString("Java Code Geeks", 50, 120);

	    // Disposes of this graphics context and releases any system resources that it is using. 
	    g2d.dispose();

	    // Save as PNG
	    File file = new File("myimage.png");
	    ImageIO.write(bufferedImage, "png", file);

	    // Save as JPEG
	    file = new File("myimage.jpg");
	    ImageIO.write(bufferedImage, "jpg", file);

	}

}

 
This was an example on how to create image file from graphics object.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

Be the First to Comment!

Notify of
avatar