JMU
Image Input and Output
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Overview
Creating Images
Reading Images
Writing Images
An Example
An Example (cont.)
javaexamples/printing/PaintJob.java
        import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * A PaintJob object is analgous to a java.awt.print.PrinterJob object
 * in that it controls the saving of "snapshot" images.
 *
 * In this case, rather than create a Paintable interface, this class
 * works with JComponent objects (all of which have a paint() method).
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class PaintJob
{
    private JComboBox             formatField;    
    private JComponent            paintable;    
    private JFileChooser          jfc;
    private JTextField            heightField, widthField;

    private static final String[] formats ={"bmp","gif","jpeg","png","tiff"};
    
    /**
     * Default Constructor
     */
    private PaintJob()
    {
       JPanel accessory, center, rows;       

       // Construct the various GUI components
       jfc = new JFileChooser();

       widthField  = new JTextField();
       heightField = new JTextField();

       formatField = new JComboBox(formats);
       formatField.setEditable(false);       

       // Layout the additional information in the JFileChooser
       accessory = new JPanel();       
       accessory.setLayout(new BorderLayout());
       accessory.add(new JLabel("Image Info", SwingConstants.CENTER), 
                     BorderLayout.NORTH);
       rows = new JPanel();
       rows.setLayout(new GridLayout(3,2));
       rows.add(new JLabel("Width:"));
       rows.add(widthField);
       rows.add(new JLabel("Height:"));
       rows.add(heightField);
       rows.add(new JLabel("Format:"));
       rows.add(formatField);
       center = new JPanel();
       center.setLayout(new BorderLayout());
       center.add(rows, BorderLayout.NORTH);
       accessory.add(center, BorderLayout.CENTER);

       jfc.setAccessory(accessory);       
    }
    

    /**
     * Creates and returns a PaintJob
     *
     * @return  A PintJobe
     */
    public static PaintJob getPaintJob()
    {
       return new PaintJob();
    }
    
    /*
     * Intructs this PaintJob what JComponent to render
     * (i.e., what JComponent to take a "snapsht" image of)
     *
     * @param component  The JComponent that will be rendered
     */
    public void setPaintable(JComponent component)
    {
       this.paintable = component;
    }
    
    /**
     * Presents a dialog to the user for changing the properties 
     * of the PaintJob
     *
     * @return true if the user does not cancel the dialog; false otherwise. 
     */
    public boolean saveDialog()
    {
       int         choice;
       Dimension   size;

       if (paintable != null)
       {
          size = paintable.getSize();
          widthField.setText(""+size.width);
          heightField.setText(""+size.width);
       }
       
       choice = jfc.showSaveDialog(paintable);
       
       return (choice == JFileChooser.APPROVE_OPTION);
    }

    /**
     * Creates and saves the "snapshot" image 
     */
    public void save() throws IOException
    {
       BufferedImage           image;
       File                    file;
       Graphics2D              g2;
       int                     height, width;       
       String                  fileType;       


       // Check to make sure everything is setup properly
       file   = jfc.getSelectedFile();
       if (file == null) throw new IOException("No File was selected.");
       
       try
       {
          width  = Integer.parseInt(widthField.getText());
          height = Integer.parseInt(heightField.getText());
       }
       catch (NumberFormatException nfe)
       {
          throw new IOException("Invalid width and/or height.");
       }

       // Construct a BufferedImage of appropriate size and get the
       // rendering engine
       image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
       g2    = image.createGraphics();
       
       
       // Render the image
       paintable.paint(g2);

       // Save the image
       fileType = (String)formatField.getSelectedItem();
       ImageIO.write(image, fileType, file);
    }
}