Class CellGeometry


  • public class CellGeometry
    extends java.lang.Object
    A class holding the information necessary to describe the geometry of a map cell. The information should be sufficient to calculate the size of a cell, an optional offset of the opaque part of an image representing the cell and the position of a cell with arbitrary coordinates on the map in pixels. Some effort has been put into the routines calculating the pixel position of a cell and for retrieving the coordinate of a cell containing a certain pixel position with regard to good accuracy.
    • Constructor Summary

      Constructors 
      Constructor Description
      CellGeometry()
      Creates a new CellGeometry object with no geometry data set.
      CellGeometry​(java.lang.String fileName)
      Creates a new CellGeometry object initializing itself with the data available in the specified resource file.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.awt.Point getCellPosition​(int mapX, int mapY)
      Returns the scaled pixel position of a cell with the map coordinates mapX and mapY.
      int getCellPositionX​(int mapX, int mapY)
      Returns the scaled pixel position on the x-axis of a cell with the map coordinates mapX and mapY.
      int getCellPositionY​(int mapX, int mapY)
      Returns the scaled pixel position on the y-axis of a cell with the map coordinates mapX and mapY.
      java.awt.Rectangle getCellRect​(int mapX, int mapY)
      Returns the scaled pixel position and size of a cell with the map coordinates mapX and mapY.
      java.awt.Dimension getCellSize()
      Returns the scaled size of cells.
      CoordinateID getCoordinate​(int sx, int sy, int z)
      Returns the map coordinate of a cell containing the pixels sx and sy.
      java.awt.Point getImageOffset()
      Only use for retrieving all Cell Geo data to consturct a related CellGeometry
      java.awt.Point getImagePosition​(int mapX, int mapY)
      Returns the scaled pixel position where to draw an image to exactly match the position of the cell with the map coordinates mapX and mapY.
      java.awt.Rectangle getImageRect​(int mapX, int mapY)
      Returns the scaled pixel position and size of an image to exactly match the position of the cell with the map coordinate mapX and mapY.
      java.awt.Dimension getImageSize()
      Returns the scaled size of images used together with this cell geometry.
      java.awt.Polygon getPolygon()
      Returns an unscaled Polygon object describing the cell hexagon.
      java.awt.Polygon getScaledPolygon()
      Returns a scaled Polygon object describing the cell hexagon.
      float getScaleFactor()
      Returns the currently set scale factor.
      float getUnscaledCellPositionX​(int mapX, int mapY)
      Returns the unscaled pixel position on the x-axis of a cell with the map coordinates mapX and mapY.
      float getUnscaledCellPositionY​(int mapX, int mapY)
      Returns the unscaled pixel position on the y-axis of a cell with the map coordinates mapX and mapY.
      void setCoordMapper​(GameSpecificStuff.CoordMapper coordMapper)
      Sets the mapper that affects how map coordinates are mapped to map coordinates.
      void setGeometry​(int[] xpoints, int[] ypoints)
      Sets the hexagon coordinates of the cell geometry, the supplied arrays are expected to be of size 6 and the values are expected to confirm with the restrictions given in the constructor description.
      void setImageOffset​(int x, int y)
      Sets the offset of the opaque part of an image representing a cell in the graphics file in pixels.
      void setImageSize​(int width, int height)
      Sets the size of graphics files used together with this cell geometry.
      void setScaleFactor​(float scaleFactor)
      Set a scale factor to be used for all calculations of cell positions and sizes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CellGeometry

        public CellGeometry()
        Creates a new CellGeometry object with no geometry data set.
      • CellGeometry

        public CellGeometry​(java.lang.String fileName)
        Creates a new CellGeometry object initializing itself with the data available in the specified resource file. The file is supposed to have the following layout:

        x0=32
        x1=63
        x2=63
        x3=32
        x4=0
        x5=0
        y0=0
        y1=16
        y2=47
        y3=63
        y4=47
        y5=16
        imgOffsetx=8
        imgOffsety=8
        imgSizex=80
        imgSizey=80

        These values are default values if they cannot be retrieved from the file. The coordinates are interpreted to describe a symmetric hexagon that sits at the coordinate axes with positive x values increasing to the right and positive y values increasing downwards. The x0 through x5 values describe the x values of the points that make up the hexagonal cell, y0 through y5 are the respective y values. x0, y0 represent the top node of the hexagon, the other points are numbered up clockwise. Therefore x4, x5 and y0 are expected to always be 0, else no guarantee can be made as to the correctness of the position calculations. The imgOffset and imgSize values give additional information about how the hexagon is located within the graphics files used together with this cellgeometry. This information is necessary to calculate the images' pixel positions for painting. imgOffset is the offset of the opaque part of the image representing a cell within the graphics file, imgSize denotes the size of the graphics files in pixels. The created CellGeometry is fully initialized with this data, no further information has to be given.
        Parameters:
        fileName - the name of the file to read the geometry data from. fileName is retrieved from the current resource bundle.
    • Method Detail

      • setGeometry

        public void setGeometry​(int[] xpoints,
                                int[] ypoints)
        Sets the hexagon coordinates of the cell geometry, the supplied arrays are expected to be of size 6 and the values are expected to confirm with the restrictions given in the constructor description.
      • setImageOffset

        public void setImageOffset​(int x,
                                   int y)
        Sets the offset of the opaque part of an image representing a cell in the graphics file in pixels. The x and y values are expected to be non-negative and smaller than the size of the cell hexagon.
      • getImageOffset

        public java.awt.Point getImageOffset()
        Only use for retrieving all Cell Geo data to consturct a related CellGeometry
      • setImageSize

        public void setImageSize​(int width,
                                 int height)
        Sets the size of graphics files used together with this cell geometry.
      • setScaleFactor

        public void setScaleFactor​(float scaleFactor)
        Set a scale factor to be used for all calculations of cell positions and sizes.
        Parameters:
        scaleFactor - The new factor. Must be > 0.
        Throws:
        java.lang.IllegalArgumentException - if scaleFactor <= 0.
      • getScaleFactor

        public float getScaleFactor()
        Returns the currently set scale factor.
      • getPolygon

        public java.awt.Polygon getPolygon()
        Returns an unscaled Polygon object describing the cell hexagon.
      • getScaledPolygon

        public java.awt.Polygon getScaledPolygon()
        Returns a scaled Polygon object describing the cell hexagon.
      • getCellSize

        public java.awt.Dimension getCellSize()
        Returns the scaled size of cells.
      • getImagePosition

        public java.awt.Point getImagePosition​(int mapX,
                                               int mapY)
        Returns the scaled pixel position where to draw an image to exactly match the position of the cell with the map coordinates mapX and mapY.
      • getImageSize

        public java.awt.Dimension getImageSize()
        Returns the scaled size of images used together with this cell geometry.
      • getImageRect

        public java.awt.Rectangle getImageRect​(int mapX,
                                               int mapY)
        Returns the scaled pixel position and size of an image to exactly match the position of the cell with the map coordinate mapX and mapY.
      • getUnscaledCellPositionX

        public float getUnscaledCellPositionX​(int mapX,
                                              int mapY)
        Returns the unscaled pixel position on the x-axis of a cell with the map coordinates mapX and mapY.
      • getUnscaledCellPositionY

        public float getUnscaledCellPositionY​(int mapX,
                                              int mapY)
        Returns the unscaled pixel position on the y-axis of a cell with the map coordinates mapX and mapY.
      • getCellPositionX

        public int getCellPositionX​(int mapX,
                                    int mapY)
        Returns the scaled pixel position on the x-axis of a cell with the map coordinates mapX and mapY.
      • getCellPositionY

        public int getCellPositionY​(int mapX,
                                    int mapY)
        Returns the scaled pixel position on the y-axis of a cell with the map coordinates mapX and mapY.
      • getCellPosition

        public java.awt.Point getCellPosition​(int mapX,
                                              int mapY)
        Returns the scaled pixel position of a cell with the map coordinates mapX and mapY.
      • getCellRect

        public java.awt.Rectangle getCellRect​(int mapX,
                                              int mapY)
        Returns the scaled pixel position and size of a cell with the map coordinates mapX and mapY.
      • getCoordinate

        public CoordinateID getCoordinate​(int sx,
                                          int sy,
                                          int z)
        Returns the map coordinate of a cell containing the pixels sx and sy. The z-value of the returned coordinate is z.