imageio

Create image file from graphics object

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: May 9th, 2013
1 3,547 1 minute read

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 newBufferedImage.
  • Create aGraphics2D usingcreateGraphics.
  • Create anew File("myimage.png").
  • UseImageIO.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 aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: May 9th, 2013
1 3,547 1 minute read
Photo of 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 atJava Code Geeks.

Related Articles

Compress a JPEG file

November 11th, 2012

Determine format of an image

November 11th, 2012

Read image from file

November 11th, 2012
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.