package visual.statik.sampled;

import java.io.*;
import javax.microedition.lcdui.*;

/**
 * A utility class for constructing/creating 
 * visual.statik.sampled.Content objects
 *
 * Changes in J2ME Version:
 *
 *    Completely re-written
 * 
 * @author Prof. David Bernstein, James Madison University
 * @version 1.0 (J2ME)
 */
public class ContentFactory
{
    private ImageFactory              imageFactory;
    
    
    /**
     * Default Constructor
     */
    public ContentFactory()
    {
        super();
        imageFactory = new ImageFactory();
    }
    
    /**
     * Read a row-oriented array of Content objects from a file
     * 
     * 
     * 
     * @param file     The name of the file (without a path)
     * @param n        The number of images
     * @param channels IGNORED!
     * @return The images or null if an Exception was thrown
     */
    public Content[] createContents(String file, int n, int channels)    
    {
        Image                            composite, subimage;
        Content[]           result;
        int                              height, width;
        
        result    = null;
        composite = imageFactory.createBufferedImage(file);
        
        if (composite != null)
        {
            height = composite.getHeight();
            width  = composite.getWidth() / n;
            result = new Content[n];
            for (int j=0; j<n; j++)
            {
                    subimage  = Image.createImage(composite, 
                                                     j*width, 0, 
                                                     width, height,
                                                     0);
                    result[j] = new Content(subimage, 0, 0);
            }
        }
        return result;
    }    

    
    /**
     * Read a table of Content objects from a file
     * 
     * 
     * 
     * @param file     The name of the file (without a path)
     * @param rows     The number of rows
     * @param columns  The number of columns
     * @param channels IGNORED!
     * @return The images or null if an Exception was thrown
     */
    public Content[][] createContents(String file, int rows, int columns, int channels)    
    {
        Image                            composite, subimage;
        Content[][]         result;
        int                              height, width;
        
        result    = null;
        composite = imageFactory.createBufferedImage(file);
        
        if (composite != null)
        {
            height = composite.getHeight() / rows;
            width  = composite.getWidth() / columns;
            result = new Content[rows][columns];
            for (int i=0; i<rows; i++)
            {
                for (int j=0; j<columns; j++)
                {
                    subimage  = Image.createImage(composite, 
                                                     j*width, i*height, 
                                                     width, height,
                                                     0);
                    result[i][j] = new Content(subimage, 0, 0);
                }
            }
        }
        return result;
    }    
        
    
    /**
     * Create a Content from a file
     * 
     * 
     * 
     * @param file   The name of the file (without a path)
     * @return The image or null if an Exception was thrown
     */
    public Content createContent(String file)    
    {
       Image                           content;
       Content         result;

       result = null;
       content = imageFactory.createBufferedImage(file);
       if (content != null)
               result = new Content(content, 0, 0);
       
       return result;       
    }    
}
