import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;

public class MapPanel extends JLabel implements MouseListener {
	private final ArrayList _listeners = new ArrayList();
	private final Location _location = new Location();
	public MapPanel() {
		ImageIcon image = new ImageIcon( ClassLoader.getSystemResource("world.png") );
		setIcon( image );

		addMouseListener( this );
	}

	public void addObserver( Observer o ) {
		_listeners.add( o );
	}

	public void mouseClicked( MouseEvent e ) {
		Dimension d = getSize();
		int mx = d.width;
		int my = d.height;
		int midx = mx/2;
		int midy = my/2;
		double lat_scale = 90.0/midy;
		double lon_scale = 180.0/midx;
		int adjusted_x = e.getX() - midx;
		int adjusted_y = (e.getY() - midy)*-1;
		_location.lat = adjusted_y * lat_scale;
		_location.lon = adjusted_x * lon_scale;
		for (Iterator i=_listeners.iterator(); i.hasNext(); ) {
			Observer o = (Observer)i.next();
			o.update( null, _location );
		}
	}
	public void mouseEntered( MouseEvent e ) { }
	public void mouseExited( MouseEvent e ) { }
	public void mousePressed( MouseEvent e ) { }
	public void mouseReleased( MouseEvent e ) { }

	public class Location {
		public double lat;
		public double lon;
	}

	public static void main( String[] args ) {
		JFrame f = new JFrame();
		MapPanel p = new MapPanel();
		p.addObserver( new Observer() {
			public void update( Observable o, Object arg ) {
				Location l = (Location)arg;
				System.out.println("Lat: "+l.lat+", Lon: "+l.lon );
			}
		});
		f.getContentPane().add( p, BorderLayout.CENTER );
		f.pack();
		f.setVisible( true );
	}
}
