private void calculateGridOffset(Grid grid, GridPoint centerOn) {
		int gridWidthPx = grid.width * this.tileSize;
		int gridHeightPx = grid.height * this.tileSize;
		if (gridWidthPx <= this.gridSpace.width && gridHeightPx <= this.gridSpace.height) {
			// The grid fits in the space we have with room to spare. The
			// offsets will be used to center the grid in the space.
			this.gridXOffset = (this.gridSpace.width - gridWidthPx) / 2;
			this.gridYOffset = (this.gridSpace.height - gridHeightPx) / 2;
		} else {
			// The grid does not fit in the space we have. The offsets will be
			// used to keep the selected square centered in the view.
			this.gridXOffset = (this.gridSpace.width / 2) - (centerOn.getColumn() * this.tileSize) - (this.tileSize / 2);
			this.gridYOffset = (this.gridSpace.height / 2) - (centerOn.getRow() * this.tileSize) - (this.tileSize / 2);

			// Lock the edges of the grid to the edges of the screen when
			// selecting a square near the edge of the grid.
			if (this.gridXOffset > 0) {
				this.gridXOffset = 0;
			}
			if (this.gridYOffset > 0) {
				this.gridYOffset = 0;
			}
			if (this.gridXOffset + gridWidthPx < this.gridSpace.width) {
				this.gridXOffset = this.gridSpace.width - gridWidthPx;
			}
			if (this.gridYOffset + gridHeightPx < this.gridSpace.height) {
				this.gridYOffset = this.gridSpace.height - gridHeightPx;
			}

			// If the grid is bigger on one side but smaller in the other,
			// center on the smaller side.
			if (gridWidthPx <= this.gridSpace.width) {
				this.gridXOffset = (this.gridSpace.width - gridWidthPx) / 2;
			} else if (gridHeightPx <= this.gridSpace.height) {
				this.gridYOffset = (this.gridSpace.height - gridHeightPx) / 2;
			}
		}
	}