Source code: org/zazof/jteg/gui/CountryCanvasList.java
1 /**
2 * Class that is responsible for maintaining a list of CountryCanvas objects
3 *
4 * Will be used by BoardCanvas to keep track of all countrycanvas objects
5 *
6 *
7 * @author Yves Vandewoude (yves@stcham.org)
8 *
9 */
10
11 package org.zazof.jteg.gui;
12
13 import org.zazof.jteg.*;
14 import java.util.Vector;
15 import java.util.Enumeration;
16
17 public class CountryCanvasList {
18
19 public CountryCanvasList() {
20 countryCanvasList = new Vector(50);
21 }
22
23 /**
24 * Adds a CountryCanvas to the list
25 *
26 * @param The CountryCanvas that you wish to add
27 */
28 public void add(CountryCanvas c) {
29 countryCanvasList.add(c);
30 }
31
32 /**
33 * Returns an iterative structure by which you can iterate to the CountryCanvas elements
34 *
35 * @return An Enumeration that allows you to iterate through the countrycanvas elements
36 */
37 public Enumeration getCountryCanvasElements()
38 {
39 return countryCanvasList.elements();
40 }
41
42
43 /**
44 * Returns the country which is closes to the absolute coordinates (x,y)
45 *
46 * @param Xcoordinate of the position you currently are
47 * @param Ycoordinate of the position you currently are
48 * @return The Country closest to your current position
49 */
50 public Country getClosestCountry(int x, int y)
51 {
52 CountryCanvas c, most_closest_c;
53 boolean first = true;
54 int most_closest_distance, distance;
55 most_closest_distance = 9999999;
56 most_closest_c = null;
57 Enumeration elements = getCountryCanvasElements();
58 while (elements.hasMoreElements())
59 {
60 c = (CountryCanvas)elements.nextElement();
61 if (first)
62 {
63 first = false;
64 most_closest_c = c;
65 most_closest_distance = c.getDistanceTo(x,y);
66 }
67 else
68 {
69 distance = c.getDistanceTo(x,y);
70 if (distance < most_closest_distance)
71 {
72 // we have a country closer to the point (x,y)
73 most_closest_c = c;
74 most_closest_distance = distance;
75 }
76 }
77 }
78 return most_closest_c.getCountry();
79 }
80
81 private Vector countryCanvasList;
82 }