Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone!
I'm trying some code in Java by using Eclipse. Here I have 2 classes: Hex and Board.
XML
public class Hex {
    ...
    protected List<Point2D.Double> Points = new ArrayList<Point2D.Double>()
    ...
};

There is a constructor inside Hex class like
public Hex(Point2D.Double point, double side, int orientation);

and the declaration for Board class:
C#
public class Board extends Hex{
    ...
    private Hex hexes[][]
    ...
};

In the Board class's body I have a piece of code:
hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(4), side, orientation);

For this piece of code IDE Eclipse tells me that: "Points cannot be resolved or is not a field".
My idea is: ok, Points is a field in the Hex class, so an object like Hex[i][j] must have a field like that Points. But the situation seems to be different. How is this strange?
Anyone could tell me the true into this situation?
I'll really appreciate your help!
Posted

Interesting, I have just created these two classes based on the above (see below), and eclipse is quite happy with it. Perhaps you could post all the code in the Board class that includes the line in error.

Java
public class Hex {
    public Hex(Point2D.Double point, double side, int orientation) {}
    protected List<point2d.double> Points = new ArrayList<point2d.double>();
};
	
public class Board extends Hex{
    public Board(Point2D.Double point, double side, int orientation) {
        super(point, side, orientation);
    }
    private Hex hexes[][];
	    
    public void zig (int i, int j, double side, int orientation){	
    	hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(4), side, orientation);
    }
};
 
Share this answer
 
v3
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):
C#
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;

    /// <param name="side">length of one side of the hexagon</param>
    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()
    { }

    /// <summary>
    /// Sets internal fields and calls CalculateVertices()
    /// </summary>
    private void Initialize(double x, double y, double side, int orientation)
    {
        this.x = x;
        this.y = y;
        this.side = side;
        this.orientation = orientation;
        CalculateVertices();
    }

    /// <summary>
    /// Calculates the vertices of the hex based on orientation. Assumes that Points[0] contains a value.
    /// </summary>

    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 = short length (outside)
        //  r = long length (outside)
        //  side = length of a side of the hexagon, all 6 are equal length
        //
        //  h = sin (30 degrees) x side
        //  r = cos (30 degrees) x side
        //
        //       h
        //       ---
        //   ----     |r
        //  /    \    |
        // /      \   |
        // \      /
        //  \____/
        //
        // Flat orientation (scale is off)
        //
        //     /\
        //    /  \
        //   /    \
        //   |    |
        //   |    |
        //   |    |
        //   \    /
        //    \  /
        //     \/
        // Pointy orientation (scale is off)

        h = CalculateH(side);
        r = CalculateR(side);

        switch (orientation)
        {
            case 0://Flat orientation
                // x,y coordinates are top left point
                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://Pointy orientation
                //x,y coordinates are top center point
                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.*;

/// <summary>
/// Represents a 2D hexagonal board
/// </summary>
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;
	/// <param name="width">Board width</param>
	/// <param name="height">Board height</param>
	/// <param name="side">Hexagon side length</param>
	/// <param name="orientation">Orientation of the hexagons</param>
	public Board(int width, int height, int side, int orientation)
	{
		Initialize(width, height, side, orientation, 0, 0);
	}

	/// <param name="width">Board width</param>
	/// <param name="height">Board height</param>
	/// <param name="side">Hexagon side length</param>
	/// <param name="orientation">Orientation of the hexagons</param>
	/// <param name="xOffset">X coordinate offset</param>
	/// <param name="yOffset">Y coordinate offset</param>
	public Board(int width, int height, int side, int orientation, int xOffset, int yOffset)
	{
		Initialize(width, height, side, orientation, xOffset, yOffset);
	}
	
	/// <summary>
	/// Sets internal fields and creates board
	/// </summary>
	/// <param name="width">Board width</param>
	/// <param name="height">Board height</param>
	/// <param name="side">Hexagon side length</param>
	/// <param name="orientation">Orientation of the hexagons</param>
	/// <param name="xOffset">X coordinate offset</param>
	/// <param name="yOffset">Y coordinate offset</param>
		
	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); // short side
		double r = CalculateR(side); // long side

		//
		// Calculate pixel info..remove?
		// because of staggering, need to add an extra r/h
		double hexWidth = 0;
		double hexHeight = 0;
		switch (orientation)
		{
			case 0://Flat orientation
				hexWidth = side + h;
				hexHeight = r + r;
				this.pixelWidth = (width * hexWidth) + h;
				this.pixelHeight = (height * hexHeight) + r;
				break;
			case 1://Pointy orientation
				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;


		// i = y coordinate (rows), j = x coordinate (columns) of the hex tiles 2D plane
		for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					// Set position booleans
						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;
						}

					//
					// Calculate Hex positions
					//
					if (isTopLeft)
					{
						//First hex
						switch (orientation)
						{ 
							case 0://Flat orientation
								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)
								{
									// Calculate from hex above
									
									hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(4), side, orientation);
								}
								else
								{
									
									
									
									// Calculate from Hex to the left and need to stagger the columns
									if (j % 2 == 0)
									{
										// Calculate from Hex to left's Upper Right Vertice plus h and R offset 
										double x = hexes[i][ j - 1].Points.get(1).getX();
										double y = hexes[i][ j - 1].Points.get(1).getY();//upperRight
										x += h;
										y -= r;
										hexes[i][j] = new Hex(x, y, side, orientation);
									}
									else
									{
										// Calculate from Hex to left's Middle Right Vertice
										hexes[i][j] = new Hex(hexes[i][j - 1].Points.get(2), side, orientation);//MiddleRight
									}
								}
								break;
							case 1:
								
								if (inLeftColumn)
								{
									// Calculate from hex above and need to stagger the rows
									if (i % 2 == 0)
									{
										hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(4), side, orientation);//BottomLeft
									}
									else
									{
										hexes[i][j] = new Hex(hexes[i - 1][j].Points.get(2), side, orientation);//BottomRight
									}

								}
								else
								{
									// Calculate from Hex to the left
									double x = hexes[i][j - 1].Points.get(1).getX();//UpperRight
									double y = hexes[i][j - 1].Points.get(1).getY();//UpperRight
									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)
		{
			//
			// Quick check to see if X,Y coordinate is even in the bounding rectangle of the board.
			// Can produce a false positive because of the staggerring effect of hexes around the edge
			// of the board, but can be used to rule out an x,y point.
			//
			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)
		{
			//http://astronomy.swin.edu.au/~pbourke/geometry/insidepoly/
			//
			// Slick algorithm that checks if a point is inside a polygon.  
            //Checks how many time a line
			// origination from point will cross each side.  
            //An odd result means inside polygon.
			//
			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;
		}
}
 
Share this answer
 
Comments
Firo Atrum Ventus 17-Aug-11 23:50pm    
Please don't post this as answer, use 'Improve Question' instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900