Trying to make a Grid Like this using java and I’m assuming a 2d Array, I’m eventually going to be making this into checkers game. Making a grid is quite easy but what I am wondering is how can I visually represent this array, so for example the top row second from the left square would be like [0][2] make each square correspond an array index. Any advice would be great, I finished the first java class and I know that this is the first project we get for the next class So! im trying to figure it out, thanks for any help!
Share
Given that it’s an internal convention, it is entirely up to you: simply pick one of the corners of the grid e.g. top-lef, as your origin. (General convention is to use top-left as origin and bottom-right as [x-max][y-max].)
Your model can be anything from a single dimensional raster model or a 2d array, or even nested Lists. Choice of model entirely depends on the use-case and performance requirements. For your case — introductory homework — 2d array is a straightforward and sensible choice.
For drawing to a grid cell @ (i,j), you’ll need a method to get the canvas coordinates (x,y) of a given grid cell:
Here you have a couple of choices: should you return the center point e.g. the middle point of a black square, or its relative origin e.g. its top-left coordinates? Depends on what you are trying to do. For example, if you are going to be rendering little icons at each cell, the paint api takes the top-left coordinate of the image you are rendering, so in that case, returning the origin is sensible.
You also need to decide if you need to map from a point in the visual grid to your data. For example, user clicks in the canvas and you need to determine the corresponding data cell. (You probably don’t need this)