import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

public class AppletImage extends Applet  {
	
	private VectorImage vect_img;
	private Choice imageChoice; 
	private Button clearButton;
	private int currentImageIndex;
	public final int IMAGE_WIDTH=100;
	public final int IMAGE_HEIGHT=70; 		
				
	public void init() {
		
		vect_img=new VectorImage();
		currentImageIndex=0;
		
		addMouseListener(new MouseInterceptor());	
      	setLayout(new BorderLayout());
      	      	
      	imageChoice = new Choice();
      	imageChoice.add("ALBANIA");
 		imageChoice.add("ANDORRA");
 		imageChoice.add("AUSTRIA");
 		imageChoice.add("BELGIO");
 		imageChoice.add("BOSNIA");
 		imageChoice.add("BULGARIA");
 		imageChoice.add("DANIMARCA");
 		imageChoice.add("ESTONIA");
 		imageChoice.add("ITALIA");
 		imageChoice.add("LITUANIA");
 		imageChoice.add("MALTA");
 		imageChoice.addItemListener(new ChoiceListener());
 		
 		clearButton=new Button("Pulisci");
 		clearButton.addActionListener(new ClearListener());
 		
 		Panel menuPanel=new Panel();
 		menuPanel.add(imageChoice);
 		menuPanel.add(clearButton);
 		
 		add(menuPanel, "South");
 		
 		 		
 	}

	public void paint(Graphics g) {
		
		for (int i=0; i<vect_img.getVectorLength();i++) {
			Image curr_vect_img=vect_img.getImage(i);
			int coordX=vect_img.getImageXLocation(i);
			int coordY=vect_img.getImageYLocation(i);
			g.drawImage(curr_vect_img,coordX,coordY,IMAGE_WIDTH, IMAGE_HEIGHT, this);
		}				
	}
	
	private class MouseInterceptor implements MouseListener {
		public void mouseClicked(MouseEvent event) {
			if (vect_img.getVectorLength()>0) {
			  	int coordX=event.getX();
			  	int coordY=event.getY();
			  	boolean found=false;
			  	for (int i=0;i<vect_img.getVectorLength();i++) {
			  		int xmin=vect_img.getImageXLocation(i);
			  		int xmax=xmin+IMAGE_WIDTH;
			  		int ymin=vect_img.getImageYLocation(i);
			  		int ymax=ymin+IMAGE_HEIGHT;
			  		if ((coordX>=xmin)&&(coordX<=xmax)&&(coordY>=ymin)&&(coordY<=ymax)) {
			  			JOptionPane.showMessageDialog(null,vect_img.getImageName(i));
			  			currentImageIndex=i;
			  			found=true;
			  		}
			  	}
			  	if (!found)
			  		vect_img.setImageLocation(coordX-IMAGE_WIDTH/2,coordY-IMAGE_HEIGHT/2,currentImageIndex);
			  	repaint();
			}
   		}
  		public void mouseEntered(MouseEvent event) { }
		public void mouseExited(MouseEvent event) { }
   		public void mousePressed(MouseEvent event) { }
   		public void mouseReleased(MouseEvent event) { }
	}
	
	private class ChoiceListener implements ItemListener {
		public void itemStateChanged(ItemEvent event) {
			  String currentImage=imageChoice.getSelectedItem();
			  vect_img.addImage(getImage(getCodeBase(),"images/"+currentImage+".JPG"), currentImage);
			  int coordX=(getWidth()-IMAGE_WIDTH)/2;
			  int coordY=(getHeight()-IMAGE_HEIGHT)/2;
			  currentImageIndex=vect_img.getVectorLength()-1;
			  vect_img.setImageLocation(coordX,coordY,currentImageIndex);
			  repaint();
   		}
	}
	
	private class ClearListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			  vect_img.clearVector();
			  repaint();
   		}
	}	
	
}