Tuesday, May 7, 2013

Create image

This is a program to create an image file in Java. The image is created from oval shapes filled with different colors. The image file is saved in your current working folder where the CreateImage.java file is. In this program, you learn two important things: using BufferedImage to store image data and using the ImageIO class to save the image data to a file in your hard drive.

create image in Java

CreateImage source code:

import javax.imageio.*;
import javax.imageio.stream.*;
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.color.*;
import javax.swing.*;
import java.util.*;
class ImageShow extends Canvas{
            public void paint(Graphics g){
1                      BufferedImage bi=(BufferedImage)createImage(400,400);                 
2                      Graphics2D g2d=(Graphics2D)bi.createGraphics();                 
3                      g2d.setPaint(new Color(0,0,255));                       
4                      g2d.fill(new Ellipse2D.Double(125,100,150,200));
                        g2d.setPaint(new Color(0,100,10));
                        g2d.fill(new Ellipse2D.Double(120,50,50,100));

                        g2d.setPaint(new Color(0,100,10));
                        g2d.fill(new Ellipse2D.Double(220,50,50,100));

                        Date dat=new Date();                     
                        g2d.setPaint(new Color(255,10,10));

5                      g.drawImage(bi,0,0,400,400,null);

                        g.drawString("myimage.jpg file of this image is created in the current folder.",300,300);
6                                  ImageIO.write(bi,"jpg",new File("myimage.gif"));                                   
                        }catch(IOException e){}

class ShowProgram extends JFrame{

                                    //Set program title
                                    setTitle("Create Image");
                                    //Create ImageShow object that is a drawing area
                                    ImageShow is=new ImageShow();
                                    //Set background color of the drawing area
                                    //Add the drawing area to the center of the program window
                                    add(is, BorderLayout.CENTER);
                                    //Let the program close when the close button is clicked.
                                    //Maximize the program window
                                    setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);        
                                    //Make use the program window visible


public class CreateImage{

            public static void main(String[] args) {
                        new ShowProgram();

Code Explanation
1 The BuffereedImage object must be created to store the image data. You will use the createImage(width,height) of the container object to create the empty BufferedImage object.
2 To draw things on the BufferedImage object, the Graphics2D object must be obtained by calling the createGraphics method of the BufferedImage object.
3 Set the color of the pen.
4 Create and fill the ellipse shape.
5 Display the image on the screen.
6 Create the image file called myimage.jpg in the current working folder by calling the write(BufferedImage, Type, File) method of the ImageIO class.

No comments:

Post a Comment