Well that settles it. Something in my “very simple” code is wrong. Check this out.
import java.awt.Polygon;
public class PolyTest
{
public static void main(String[] args)
{
Polygon p = new Polygon
(
new int[]{410,419,431,452,471,486,477,462,464,481,478,460,451,444,442,431,406,401,390,386,397,404,406},
new int[]{118,102,95,91,105,117,128,121,134,140,185,228,237,237,222,211,187,170,170,154,144,137,129},
23
);
System.out.println("437 174 " + p.contains(437, 174));
System.out.println("427 112 " + p.contains(427, 112));
System.out.println("452 199 " + p.contains(452, 199));
System.out.println("474 160 " + p.contains(474, 160));
System.out.println("397 162 " + p.contains(397, 162));
System.out.println("474 114 " + p.contains(474, 114));
System.out.println("999 999 " + p.contains(999, 999));
}
}
Prints:
437 174 true
427 112 true
452 199 true
474 160 true
397 162 true
474 114 true
999 999 false
There must be something wrong in my Z-sorting. EDIT - or not. It’s Serializable’s fault I think. Check this out:
In mousePressed, a loop goes through all the Country objects (just simple structs) and finds if any are clicked. It was telling me none were, and I knew the problem “country” was Scandinavia so I basically added in a check in order to print out just info from Scandinavia. Interestingly enough, it cycles through that country just fine, and even further its coordinates are all normal. What’s doubly wacky is that if I make a copy of that Polygon right within that function, the copy contains the mouse click but the original does not. My conclusion is that it got saved to the disk in some crapped out fashion. So I’ll try not using ObjectOutputStream and see what happens.
Here’s the code, it’s pretty simple. I search backwards so that the last drawn item (the item on top) is what is clicked. You can see I added in that test if statement, and I make a new Polygon and all that right there. The regular code is the if statement just below - dead simple.
for (int i = countries.size()-1; i >= 0; i--)
{
if (countries.get(i).name.equals("Scandinavia"))
{
System.out.println("In Scandi " + e.getX() + " " + e.getY());
System.out.print("new Polygon(new int[]{");
int[] xs = new int[countries.get(i).polygon.npoints];
int[] ys = new int[xs.length];
for (int j = 0; j < countries.get(i).polygon.npoints; j++)
{
System.out.print(countries.get(i).polygon.xpoints[j] + (j < countries.get(i).polygon.npoints-1 ? "," : ""));
xs[j] = countries.get(i).polygon.xpoints[j];
}
System.out.print("}, new int[]{");
for (int j = 0; j < countries.get(i).polygon.npoints; j++)
{
System.out.print(countries.get(i).polygon.ypoints[j] + (j < countries.get(i).polygon.npoints-1 ? "," : ""));
ys[j] = countries.get(i).polygon.ypoints[j];
}
System.out.println("}," + countries.get(i).polygon.npoints + ")");
System.out.println(countries.get(i).polygon.contains(e.getX(),e.getY()));
Polygon p = new Polygon(xs,ys,xs.length);
System.out.print("new Polygon(new int[]{");
for (int j = 0; j < p.npoints; j++)
{
System.out.print(p.xpoints[j] + (j < p.npoints-1 ? "," : ""));
}
System.out.print("}, new int[]{");
for (int j = 0; j < p.npoints; j++)
{
System.out.print(p.ypoints[j] + (j < p.npoints-1 ? "," : ""));
}
System.out.println("}," + p.npoints + ")");
System.out.println(p.contains(e.getX(), e.getY()));
}
if (countries.get(i).polygon.contains(e.getX(),e.getY()))
{
System.out.println("Selected something");
selectedCountry = i;
break;
}
}
And here is the output:
In Scandi 438 166
new Polygon(new int[]{410,419,431,452,471,486,477,462,464,481,478,460,451,444,442,431,406,401,390,386,397,404,406}, new int[]{118,102,95,91,105,117,128,121,134,140,185,228,237,237,222,211,187,170,170,154,144,137,129},23)
false
new Polygon(new int[]{410,419,431,452,471,486,477,462,464,481,478,460,451,444,442,431,406,401,390,386,397,404,406}, new int[]{118,102,95,91,105,117,128,121,134,140,185,228,237,237,222,211,187,170,170,154,144,137,129},23)
true
Interesting, no? I wonder if the winding rule is messed up or something like Keith suggested. I’m not worrying about it any further, though, because like I said this just generates code for my 4k game, which doesn’t use serialization.