////////////////////////////////////////////////////////////////////////
//Roundabout.java
//author -- Tom Valesky
//purpose -- This class is an illustration of how to serialize and
//deserialize images. It loads an image, converts it to a string of
//bytes, converts it back to an image, and displays it. This class also
//makes use of the MediaTracker class, which waits for an image to be
//loaded before proceeding.
////////////////////////////////////////////////////////////////////////
import java.applet.Applet;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.util.Random;

public class Roundabout extends Applet
{
        boolean image_is_ready = false;
        Image final_image;

        public void start()
        {
                repaint();
        }

        public void paint(Graphics g)
        {
                if (image_is_ready)
                {
                        g.drawImage(final_image, 0, 0, this);
                }
                System.out.println("paint called");
        }

        public void init()
        {
                int imageWidth, imageHeight;
		Image initialImage;
                //get image
	        String imageName = getParameter("image");
        	try
                	{
                        MediaTracker mt = new MediaTracker(this);

	                URL imageURL= new URL(getCodeBase(), imageName);
        	        initialImage = getImage(imageURL);

                        //add the image to the media tracker's list
                        mt.addImage(initialImage, 1);
                        //wait for all images in list to load before proceeding
                        mt.waitForAll();

                        //serialize to bytes

                        imageWidth = initialImage.getWidth(this);
                        imageHeight = initialImage.getHeight(this);

                        int [] img_data = new int[imageWidth * imageHeight];

                        //the PixelGrabber class is used to convert
                        //Images to bytes

                        PixelGrabber pg = new PixelGrabber(initialImage, 0, 0, imageWidth, imageHeight, img_data, 0, imageWidth);

                        //copy pixels into array
                        pg.grabPixels();

                        //rebuild into image

                        //use MemoryImageSource class to create image
                        Image temp_image = createImage(new MemoryImageSource(imageWidth, imageHeight, img_data, 0, imageWidth));

                        //use MediaTracker again to wait for image to load
                        mt.addImage(temp_image, 1);
                        mt.waitForAll();

                        //set class variable to point to the image
                        final_image = temp_image;

                        //it's now OK to draw the image
                        image_is_ready = true;

                	}
	        catch (MalformedURLException mue)
        	        {
                	System.out.println("Malformed URL exception when trying to load image " + imageName);
                        image_is_ready = false;
	                }
                catch (InterruptedException ie)
                        {
                        System.out.println("Interrupted exception");
                        image_is_ready = false;
                        }




        }
}
