字段似乎没有出现在对象中 [英] A field doesn't seem to appear in an object

查看:75
本文介绍了字段似乎没有出现在对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在尝试使用Eclipse在Java中编写一些代码.在这里,我有2个班级:十六进制和董事会.

Hello everyone!
I''m trying some code in Java by using Eclipse. Here I have 2 classes: Hex and Board.

public class Hex {
    ...
    protected List<Point2D.Double> Points = new ArrayList<Point2D.Double>()
    ...
};


Hex类中有一个构造函数,如


There is a constructor inside Hex class like

public Hex(Point2D.Double point, double side, int orientation);


以及董事会课程的声明:


and the declaration for Board class:

public class Board extends Hex{
    ...
    private Hex hexes[][]
    ...
};


在Board类的正文中,我有一段代码:


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);


对于这段代码,IDE Eclipse告诉我:点无法解析或不是字段".
我的想法是:好的, Points 是Hex类中的一个字段,因此像Hex [i] [j]之类的对象必须具有类似于Points的字段.但是情况似乎有所不同.这怎么奇怪?
任何人都可以告诉我这种情况的真实情况吗?
非常感谢您的帮助!


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!

推荐答案

有趣的是,我刚刚根据上面的内容创建了这两个类(请参见下文),而eclipse对此非常满意.它.也许您可以在Board类中发布包含错误行的所有代码.

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.

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);
    }
};


Richard MacCutchan!
感谢您在可爱的Eclipse中尝试我的代码.
根据您的建议,将我的代码发布在这里:
这是Hex类(用于存储Hexagon):
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;

    /// <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;

        }

    }

}



这是Board类的代码(它被用作许多Hexagon的集合).顺便说一下,我正在尝试创建一个Hexboard游戏.



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;
		}
}


这篇关于字段似乎没有出现在对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆