Need help implmenting this Java class SquareMap Every Cell (the colored squares)
ID: 3827570 • Letter: N
Question
Need help implmenting this Java class
SquareMap
Every Cell (the colored squares) in the SquareMap has an column and row index into a 2D array holding the cells. In Fig. 5, the top left cell has the index (column, row) = (0, 0), and its neighbor to the right has (1, 0), and so on. The neighbors for a cell at index (column, row) shall constitute the following if they're within the map: 1. The cell at index (col-1, row) 2. The cell at index (col+1, row) 3. The cell at index (col, row-1) 4. The cell at index (col, row+1) Depending on the cell's index in the 2D array, it can have as few as two neighbors and as many as four. (Note: here we're describing cell indices using the pairs (horizontal, vertical) as in mathematical coordinates. Remember when you access elements of the 2D array, you have to reverse them, since for a 2D array the row index is first.)
In addition to its column and row index, each cell is associated with a location in pixels within a graphical window. Pixels are measured from the upper left corner. The GraphMap class includes an attribute distance representing the size in pixels of each Cell. When creating Polygons for each cell, the coordinates in pixels shall follow the scheme shown in Fig. 5. There shall be an empty border around the cells with width of distance/2. The createPolygon method then shall return a square with width distance. The orange dot in Fig. 5 represents the point (0, 0) in pixels. For any point in pixels, the selectClosetIndex method shall return the cell's column and row if the given (x, y) is within its square. For example, focus on the pink dot in Fig. 5:
Let's say that distance equals 10, and let's say that the pink dot belongs to the point (19, 21) in pixels. Then the closest column and row would be (1, 1). A point on the boundary between cells is assumed to belong to the square to the right and/or below. Finally, the width of the GraphMap in pixels shall equal the sum of the widths of the polygons we need to draw in a row, plus the empty border on either side of the row. The height shall work similarly.
**A square lattice where each cell has 4 neighbors set up in a checker board pattern.
**/
public class SquareMap extends GraphMap implements Updatable,Iterable<Cell>
{
public SquareMap()
{
}
@Override
public void update()
{
// TODO Auto-generated method stub
}
//Gets the width of the window in pixels for rendering, including the border area.
//Return: The width in pixels
@Override
public int getPixelWidth()
{
// TODO Auto-generated method stub
return 0;
}
//Gets the height of the window in pixels for rendering, including the border area.
//Return: the height in pixels
@Override
public int getPixelHeight()
{
// TODO Auto-generated method stub
return 0;
}
//Create an array of neighbors for the cell with given column and row.
//Return: An array containing adjacent cells
@Override
public Cell[] createNeighbors(int col, int row)
{
// TODO Auto-generated method stub
return null;
}
//Get the column and row indices for the cell closest to a given pixel (x, y)
//coordinate, returned as a Point object in which x is the column and y is the row.
//Return: column and row indices for the cell closest to the given (x, y)
@Override
protected Point selectClosestIndex(int x, int y)
{
// TODO Auto-generated method stub
return null;
}
//Create a polygon for the cell with the given column and row.
//Returns: A polygon with correct pixel coordinates for rendering the cell
@Override
public Polygon createPolygon(int col, int row)
{
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
import graph.Cell;
import graph.GraphMap;
import java.awt.*;
import java.util.ArrayList;
public class SquareMap extends GraphMap {
public SquareMap() {
}
/**
* Calculates the width of map
*
* @return the width in pixels of the map
*/
@Override
public int getPixelWidth() {
// multiplies the number of columns by pixel length plus 1 pixel to
// account for the border
return getCells()[0].length * getDistance() + getDistance();
}
/**
* Calculates the height of map
*
* @return the height in pixels of the map
*/
@Override
// multiplies the nubmer of rows by the pixel length +1 pixel to account for
// the border
public int getPixelHeight() {
return getCells().length * getDistance() + getDistance();
}
/**
* Creates an array with the cells around the given index
*
* @param col
* The column index of a Cell
* @param row
* The row index of a Cell
* @return array of the cells above, below, to the left of, and to the right
* of the cell in the given position
*/
@Override
public Cell[] createNeighbors(int col, int row) {
// Creates an ArrayList of Cells that will hold the cell objects
ArrayList<Cell> neighbors = new ArrayList<Cell>();
// Checks that it has cells above it, if it does add the Cell above it
// to neighbors
if (row > 0) {
neighbors.add(getCells()[row - 1][col]);
}
// If the row is less than the number of rows (minus 1 to account for
// the 0 index), then get the value from the Cell from the next row
if (row < getCells().length - 1) {
neighbors.add(getCells()[row + 1][col]);
}
// If there are cells to the left of it, add the cell to the left to
// neighbors
if (col > 0) {
neighbors.add(getCells()[row][col - 1]);
}
// if there are cells to the right of it add the cell to the right
if (col < getCells()[0].length - 1) {
neighbors.add(getCells()[row][col + 1]);
}
// return the neighbors arrayList to a Cell array with the same number
// of items as in the arraylist
return neighbors.toArray(new Cell[neighbors.size()]);
}
/**
* Find the closest Point to the given coordinantes
*
* @param x
* The x coordinate in pixels
* @param y
* The y coordinate in pixels
* @return the closest coordinate to the specified in
*/
@Override
protected Point selectClosestIndex(int x, int y) {
// subtract the white space and divide by the pixel distance
int xIndex = (x - getDistance() / 2) / getDistance();
if (xIndex > getCells().length - 1) {
xIndex = getCells().length - 1;
}
int yIndex = (y - getDistance() / 2) / getDistance();
if (yIndex > getCells()[0].length - 1) {
yIndex = getCells()[0].length - 1;
}
return new Point(xIndex, yIndex);
}
/**
* Creates square given the top left vertex of the square
*
* @param col
* The column index of a Cell
* @param row
* The row index of a Cell
* @return a new Polygon with the arguments of the x and y vertecies and the
* number of verticies
*/
@Override
public Polygon createPolygon(int col, int row) {
// create new arrays to hold the points
int[] xCoor = new int[4];
int[] yCoor = new int[4];
// loop through the arrays and assign values that would create a square
for (int i = 0; i < 4; i++) {
int coor = col * getDistance() + getDistance() / 2;
if (i == 0 || i == 3) {
xCoor[i] = coor;
} else if (i == 1) {
xCoor[i] = coor + getDistance();
} else if (i == 2) {
xCoor[i] = coor + getDistance();
}
}
for (int i = 0; i < 4; i++) {
int coor = row * getDistance() + getDistance() / 2;
if (i == 0 || i == 1) {
yCoor[i] = coor;
}
if (i == 2) {
yCoor[i] = coor + getDistance();
}
if (i == 3) {
yCoor[i] = coor + getDistance();
}
}
// we return a new Polygon based on the arrays of coordinates generated,
// there are 4 endpoints because a square only has 4 vertecies
return new Polygon(xCoor, yCoor, 4);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.