Hi Richard MacCutchan!
Thank you for trying my code in your lovely Eclipse.
According to your suggestion to post my code in here:
This is Hex class (it's used to store a Hexagon):
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Hex {
private List<Point2D.Double> Points = new ArrayList<Point2D.Double>();
private double side;
private double h;
private double r;
private int orientation;
private double x;
private double y;
public Hex(double x, double y, double side, int orientation)
{
Initialize(x, y, side, orientation);
}
public Hex(Point2D.Double point, double side, int orientation)
{
Initialize(point.getX(), point.getY(), side, orientation);
}
public Hex()
{ }
private void Initialize(double x, double y, double side, int orientation)
{
this.x = x;
this.y = y;
this.side = side;
this.orientation = orientation;
CalculateVertices();
}
private static double CalculateH(double side)
{
return Math.sin(Math.toRadians(30))*side;
}
public static double CalculateR(double side)
{
return Math.cos(Math.toRadians(30))*side;
}
private void CalculateVertices()
{
h = CalculateH(side);
r = CalculateR(side);
switch (orientation)
{
case 0:
Points.add(new Point2D.Double(x, y));
Points.add(new Point2D.Double(x + side, y));
Points.add(new Point2D.Double(x + side + h, y + r));
Points.add(new Point2D.Double(x + side, y + r + r));
Points.add(new Point2D.Double(x, y + r + r));
Points.add(new Point2D.Double(x - h, y + r));
break;
case 1:
Points.add(new Point2D.Double(x, y));
Points.add(new Point2D.Double(x + r, y + h));
Points.add(new Point2D.Double(x + r, y + side + h));
Points.add(new Point2D.Double(x, y + side + h + h));
Points.add(new Point2D.Double(x - r, y + side + h));
Points.add(new Point2D.Double(x - r, y + h));
break;
default:
break;
}
}
}
And here is the code for Board class (it's used as a collection of many Hexagons). By the way, I'm trying to creat a hexboard game.
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
public class Board{
private Hex hexes[][];
private int width;
private int height;
private int xOffset;
private int yOffset;
private double side;
private double pixelWidth;
private double pixelHeight;
private int orientation;
public Board(int width, int height, int side, int orientation)
{
Initialize(width, height, side, orientation, 0, 0);
}
public Board(int width, int height, int side, int orientation, int xOffset, int yOffset)
{
Initialize(width, height, side, orientation, xOffset, yOffset);
}
private void Initialize(int width, int height, int side, int orientation, int xOffset, int yOffset)
{
this.width = width;
this.height = height;
this.xOffset = xOffset;
this.yOffset = yOffset;
this.side = side;
this.orientation = orientation;
double h = CalculateH(side);
double r = CalculateR(side);
double hexWidth = 0;
double hexHeight = 0;
switch (orientation)
{
case 0:
hexWidth = side + h;
hexHeight = r + r;
this.pixelWidth = (width * hexWidth) + h;
this.pixelHeight = (height * hexHeight) + r;
break;
case 1:
hexWidth = r + r;
hexHeight = side + h;
this.pixelWidth = (width * hexWidth) + r;
this.pixelHeight = (height * hexHeight) + h;
break;
default:
break;
}
boolean inTopRow = false;
boolean inBottomRow = false;
boolean inLeftColumn = false;
boolean inRightColumn = false;
boolean isTopLeft = false;
boolean isTopRight = false;
boolean isBotomLeft = false;
boolean isBottomRight = false;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == 0)
{
inTopRow = true;
}
else
{
inTopRow = false;
}
if (i == height - 1)
{
inBottomRow = true;
}
else
{
inBottomRow = false;
}
if (j == 0)
{
inLeftColumn = true;
}
else
{
inLeftColumn = false;
}
if (j == width - 1)
{
inRightColumn = true;
}
else
{
inRightColumn = false;
}
if (inTopRow && inLeftColumn)
{
isTopLeft = true;
}
else
{
isTopLeft = false;
}
if (inTopRow && inRightColumn)
{
isTopRight = true;
}
else
{
isTopRight = false;
}
if (inBottomRow && inLeftColumn)
{
isBotomLeft = true;
}
else
{
isBotomLeft = false;
}
if (inBottomRow && inRightColumn)
{
isBottomRight = true;
}
else
{
isBottomRight = false;
}
if (isTopLeft)
{
switch (orientation)
{
case 0:
hexes[0][0] = new Hex(0 + h + xOffset, 0 + yOffset, side, orientation);
break;
case 1:
hexes[0][0] = new Hex(0 + r + xOffset, 0 + yOffset, side, orientation);
break;
default:
break;
}
}
else
{
switch (orientation)
{
case 0:
if (inLeftColumn)
{
hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(4), side, orientation);
}
else
{
if (j % 2 == 0)
{
double x = hexes[i][ j - 1].Points.get(1).getX();
double y = hexes[i][ j - 1].Points.get(1).getY();
x += h;
y -= r;
hexes[i][j] = new Hex(x, y, side, orientation);
}
else
{
hexes[i][j] = new Hex(hexes[i][j - 1].Points.get(2), side, orientation);
}
}
break;
case 1:
if (inLeftColumn)
{
if (i % 2 == 0)
{
hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(4), side, orientation);
}
else
{
hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(2), side, orientation);
}
}
else
{
double x = hexes[i][j - 1].Points.get(1).getX();
double y = hexes[i][j - 1].Points.get(1).getY();
x += r;
y -= h;
hexes[i][j] = new Hex(x, y, side, orientation);
}
break;
default:
break;
}
}
}
}
}
private static double CalculateH(double side)
{
return Math.sin(Math.toRadians(30))*side;
}
public static double CalculateR(double side)
{
return Math.cos(Math.toRadians(30))*side;
}
public boolean PointInBoardRectangle(Point2D.Double point)
{
return PointInBoardRectangle(point.getX(),point.getY());
}
public boolean PointInBoardRectangle(double x, double y)
{
int topLeftX = 0 + xOffset;
int topLeftY = 0 + yOffset;
double bottomRightX = topLeftX + pixelWidth;
double bottomRightY = topLeftY + pixelHeight;
if (x > topLeftX && x < bottomRightX && y > topLeftY && y < bottomRightY)
{
return true;
}
else
{
return false;
}
}
public Hex FindHexMouseClick(Point2D.Double point)
{
return FindHexMouseClick(point.getX(),point.getY());
}
public Hex FindHexMouseClick(double x, double y)
{
Hex target = null;
if (PointInBoardRectangle(x, y))
{
for (int i = 0; i < hexes.length; i++)
{
for (int j = 0; j < hexes[0].length; j++)
{
if (InsidePolygon(hexes[i][j].Points, 6, new Point2D.Double(x, y)))
{
target = hexes[i][j];
break;
}
}
if (target != null)
{
break;
}
}
}
return target;
}
public static boolean InsidePolygon(List<Point2D.Double> polygon, int N, Point2D.Double p)
{
int counter = 0;
int i;
double xinters;
Point2D.Double p1,p2;
p1 = polygon.get(0);
for (i=1;i<=N;i++)
{
p2 = polygon.get(i % N);
if (p.getY() > Math.min(p1.getY(), p2.getY()))
{
if (p.getY() <= Math.max(p1.getY(),p2.getY()))
{
if (p.getX() <= Math.max(p1.getX(),p2.getX()))
{
if (p1.getY() != p2.getY())
{
xinters = (p.getY()-p1.getY())*(p2.getX()-p1.getX())/(p2.getY()-p1.getY())+p1.getX();
if (p1.getX() == p2.getX() || p.getX() <= xinters)
counter++;
}
}
}
}
p1 = p2;
}
if (counter % 2 == 0)
return false;
else
return true;
}
}