/*
	Java Applet showing pictures of Prag and Spain
	                     By
		     Gianluca Interlandi
		     
		       February 2001
		       
       A basic extension of the java.applet.Applet class
*/


import java.awt.*;
import java.applet.*;


public class travels extends Applet
{
    // Declarations for the window
    static Frame PicWindow = new Frame("Show Images");
    int frame_width=1200;
    int frame_height=800;

    Label[] lTravel = new Label[3];

    final int max=20;
    int n;
    String travel;
    String[] pictureName = new String[max];
    ImageCanvas[] thumbComponent = new ImageCanvas[max];
    Image thumbnail[] = new Image[max];
    Image picture[] = new Image[max];
    ImageCanvas picComponent;
    
    public void init()
	{
	    //Applet(this);
		setLayout(null);
		//setSize(578,426);
		//setSize(800,1200);
		setSize(0,0);
		setBackground(new Color(16777215));
		PicWindow.setLayout(null);
		PicWindow.setBackground(new Color(39423));
		//PicWindow.setBackground(new Color(22015));
		//PicWindow.setBounds(0,0,0,0);
		//PicWindow.setVisible(true);
		//PicWindow.setVisible(false);		

		SymMouse aSymMouse = new SymMouse();
		lTravel[0] = new Label("Prag");
		lTravel[1] = new Label("Barcelona");
		lTravel[2] = new Label("Madrid");
		for (int t=0;t<3;t++)
		    {
			PicWindow.add(lTravel[t]);
			lTravel[t].addMouseListener(aSymMouse);
			lTravel[t].setBounds(10,30+20*t,60,20);
		    }
		// Load the thumbnails
		travel = "Prag";
		n=10;
		setNames();
		loadThumbs();
                PicWindow.setBounds(0,0,frame_width, frame_height);
		ShowThumbs();
		// Load the pictures
		//loadPictures();

		// REGISTER_LISTENERS
		//SymKey aSymKey = new SymKey();
 		//tMatchString.addKeyListener(aSymKey);
 		WinAction lWinAction = new WinAction();
 		PicWindow.addWindowListener(lWinAction);
 
    }


    //Start the applet (after init)
    public void start() {
	//PicWindow.setBounds(0,0,frame_width, frame_height);
	PicWindow.setVisible(true);
	//ShowThumbs();
    }

    //Stop the applet
    public void stop() {
        //PicWindow.setVisible(false);
    }

    //Destroy the applet
    public void destroy() {
        //PicWindow.setVisible(false);
    }

    // Method to determine which pictures are in this travel
    public void setNames ()
    {
	if (travel == "Prag")
	    {
		pictureName[0] = new String("prag_museum");
		pictureName[1] = new String("prag_museumview");
		pictureName[2] = new String("prag_place");
		pictureName[3] = new String("prag_church");
		pictureName[4] = new String("prag_place2");
		pictureName[5] = new String("prag_stairs");
		pictureName[6] = new String("prag_view");
		pictureName[7] = new String("prag_church2");
		pictureName[8] = new String("prag_golden");
	    }
	if (travel == "Barcelona")
	    {
		travel = new String("Spain");
		pictureName[0] = new String("barcelona_gotico");
		pictureName[1] = new String("barcelona_hotel");
		pictureName[2] = new String("barcelona_pedrera");
		pictureName[3] = new String("barcelona_gamblas");
		pictureName[4] = new String("barcelona_sagrada02");
		pictureName[5] = new String("barcelona_sagrada1");
		pictureName[6] = new String("barcelona_sagrada2");
		pictureName[7] = new String("barcelona_sagrada3");
		pictureName[8] = new String("barcelona_plazaespana");
 		pictureName[9] = new String("barcelona_antenna1");
// 		pictureName[10] = new String("barcelona_antenna2");
// 		pictureName[11] = new String("barcelona_parqueguell1");
// 		pictureName[12] = new String("barcelona_parqueguell2");
	    }
	if (travel == "Madrid")
	    {
		travel = new String("Spain");
		pictureName[0] = new String("madrid_orso");
		pictureName[1] = new String("madrid_plazamayor1");
		pictureName[2] = new String("madrid_plazamayor2");
		pictureName[3] = new String("madrid_palacioreal1");
		pictureName[4] = new String("madrid_palacioreal2");
		pictureName[5] = new String("madrid_palacioreal3");
		pictureName[6] = new String("madrid_correos");
		pictureName[7] = new String("madrid_prado1");
		pictureName[8] = new String("madrid_prado2");
	    }
	
    }


    // Method that preloads the thumbnails
    public void loadThumbs()
    {
	String path="../picts/Thumbnails/";
	MediaTracker tracker = new MediaTracker(this);
	for (int i=0;i<n;i++) {
	    thumbnail[i] = getImage(getCodeBase(),path+pictureName[i]+"_tn.jpg");
	// Add the thumbnail to the image tracker
	// This allows to load all images immediately
	    tracker.addImage(thumbnail[i],0);
	}
	// Start downloading the images. Wait until they are loaded.
	try {
	    tracker.waitForAll();
	}
	catch (InterruptedException e) {}
    }
    
	// Method that preloads the pictures
    public void loadPictures()
    {
	String path="../picts/"+travel+"/";
	for (int i=0;i<n;i++)
	    picture[i] = getImage(getCodeBase(),path+pictureName[i]+".jpg");
    }


    // This method displays the thumbnails at the bottom of the window
    public void ShowThumbs() 
    {
	// Some variable declarations
	int w,h,y;
	int x=5;
	//String path="../picts/Thumbnails/";
	//Image thumbnail[] = new Image[n];
	SymMouse aSymMouse = new SymMouse();

	// Show the thumbnails

	for (int i=0;i<n;i++)
	    {
		// Getting the width and the height of the thumbs
		
		w=136;
		h=136;
		if (thumbnail[i] != null)
		    {
			int width=thumbnail[i].getWidth(this);
			if (width != -1)
			    w=width;
			int height=thumbnail[i].getHeight(this);
			if (height != -1)
			    h=height;
		    }
		
		//System.out.println(thumbnail[i].getHeight(this));
		
		// Creating the canvas that will display the thumbnail
		thumbComponent[i]=new ImageCanvas(thumbnail[i],w,h);
		
		// Setting the location of the image
		y=frame_height-h-5;
		thumbComponent[i].setBounds(x,y,w,h);
		x+=w+6;
		
		// Adding listeners
		thumbComponent[i].addMouseListener(aSymMouse);
		PicWindow.add(thumbComponent[i]);
		//add(thumbComponent[i]);

		// Load the pictures after the thumbnails habe been drawn
		loadPictures();
		showPicture(0);
	    }

    }
    
    // Method that shows the enlarged picture
    public void showPicture(int i)
    {
	int x,y,w,h;
	MediaTracker tracker = new MediaTracker(this);
 	if (picComponent != null)
 	    {
 		picComponent.setVisible(false);
 		picComponent=null;
 	    }

	// Add the picture to the image tracker
	// This allows to load all images immediately
	tracker.addImage(picture[i],i);
	// Start downloading the image. Wait until it is loaded.
	//try {
	//tracker.waitForID(i);
	//}
	//catch (InterruptedException e) {}
	tracker.checkID(i);
	
	w=624;
	h=624;
	if (picture[i] != null)
	    {
		int width=picture[i].getWidth(this);
		if (width != -1)
		    w=width;
		int height=picture[i].getHeight(this);
		if (height != -1)
		    h=height;
	    }
	
	x=(frame_width-w)/2;
	y=30;
	picComponent = new ImageCanvas(picture[i],w,h);
	picComponent.setBounds(x,y,w,h);
	PicWindow.add(picComponent);
	picComponent.setVisible(true);
	//add(picComponent);
    }
    
    
    // Class to create a component that shows the images
    class ImageCanvas extends Canvas
    {
	Image image;
	boolean trueSizeKnown = false;
	int width,height;
	Dimension minSize;

	public ImageCanvas(Image image,int width, int height)
	{
	    if (image == null)
		{
		    System.err.println("Canvas got invalid image object!");
		    return;
		}
	    this.image=image;
	    this.width = width;
	    this.height = height;
	    minSize = new Dimension(width,height);
	}

	public Dimension getPreferredSize()
	{
	    return getMinimumSize();
	}

	public synchronized Dimension getMinimumSize()
	{
	    return minSize;
	}

	public void paint (Graphics g)
	{
	    if (image != null)
		{
		    if (!trueSizeKnown)
			{
			    int imageWidth = image.getWidth(this);
			    int imageHeight = image.getHeight(this);
			    if ((imageWidth > 0) && (imageHeight > 0))
				{
				    trueSizeKnown = true;
				    // Component initiated resizing
				    width = imageWidth;
				    height = imageHeight;
				    minSize = new Dimension(width,height);
				    if (width > 150) {
					int x = (frame_width-width)/2;
					int y =30;
					setBounds(x,y,width,height);
				    }
				    setSize(width,height);
				    g.drawImage(image,0,0,this);
				    g.drawRect(0,0,width-1,height-1);
				}
			}
		    
		    else
			{
			    setSize(width,height);
			    g.drawImage(image,0,0,this);
			    //g.drawRect(0,0,width-1,height-1);
			}
		}
	}
	
    }


    // Class that implements that the window can be closed
    class WinAction implements java.awt.event.WindowListener
    {
        public void windowClosing (java.awt.event.WindowEvent e)
        {
            if (e.getID() == java.awt.event.WindowEvent.WINDOW_CLOSING)
            {
                if (e.getSource() == PicWindow) {
                    //deleteAllPictures();
                    PicWindow.setVisible(false);
		    }
            }
        }
        public void windowOpened(java.awt.event.WindowEvent e) {}
        public void windowClosed(java.awt.event.WindowEvent e) {}
        public void windowIconified(java.awt.event.WindowEvent e) {}
        public void windowDeiconified(java.awt.event.WindowEvent e) {}
        public void windowActivated(java.awt.event.WindowEvent e) {}
        public void windowDeactivated(java.awt.event.WindowEvent e) {}
    }


    // Class that implements that a mouse action can be performed
    class SymMouse extends java.awt.event.MouseAdapter
    {
        public void mouseClicked(java.awt.event.MouseEvent event)
	{
		    Object object = event.getSource();
		    for (int i=0;i<n;i++)
			{
			    if (object == thumbComponent[i])
				{
				    showPicture(i);
				    //for (int j=0;j<n;j++)
				    //thumbComponent[j].setVisible(false);
				    //ShowThumbs();
				}
			}
		    if (object == lTravel[0])
			{
			    travel = "Prag";
			    n=9;
			    reshowThumbs();
			}
		    if (object == lTravel[1])
			{
			    travel = "Barcelona";
			    n=10;
			    reshowThumbs();
			}
		    if (object == lTravel[2])
			{
			    travel = "Madrid";
			    n=9;
			    reshowThumbs();
			}
	}

	public void reshowThumbs()
	{
	    for (int j=0;j<n;j++) {
		thumbComponent[j].setVisible(false);
		thumbComponent[j] = null;
	    }
	    setNames();
	    loadThumbs();
	    ShowThumbs();
	}   
	
	public void mouseEntered(java.awt.event.MouseEvent event)
	{
	    Object object = event.getSource();
	    for (int i=0;i<n;i++)
		{
		    if (object == thumbComponent[i])
			thumbComponent[i].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		}
	    for (int t=0;t<3;t++)
		if (object == lTravel[t])
		    lTravel[t].setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	}
    }

}

