How to draw my android Hexagons in a grid?

| | August 5, 2015

I am trying to create a hexagon grid using the Android ’Canvas’ package. I have a working
’Hexagon’ class that when used in a view will draw a single hexagon on screen. The problem
is that I need many – a hundred or so – of these hexagons. They are small. Very small.The
goal is to draw a bundle of them that I refer to as sectors. I have created my HexGrid class:

package com.jackson.olden.game;

import android.view.*;
import android.graphics.*;

public class HexGrid
{
    private Hexagon hexMetrics;

    public static int rows;
    public static int columns;

    public HexGrid(View view, int rows, int columns)
    {
        this.columns = columns;
        this.rows = rows;

        hexMetrics = new Hexagon(view, 480, 480);
    }

    public void drawSector(Canvas c, int rowColor, int columnColor)
    {
    draw(c, rowColor, columnColor);
    }

    public void drawSector(Canvas c, int gridColor)
    {
        draw(c, gridColor);
    }

    public void draw(Canvas c, int rowColor, int columnColor)
    {
        drawColumns(c, columnColor);
        drawRows(c, rowColor);
    }

    public void draw(Canvas c, int gridColor)
    {
        drawColumns(c, gridColor);
        drawRows(c, gridColor);
    }

    public void drawRows(Canvas c, int color)
    {
        for(int i = 0; i < rows; i++){
            hexMetrics.draw(c, color);
    }
}

    public void drawColumns(Canvas c, int color)
    {
        for(int i = 0; i < columns; columns++){
            hexMetrics.draw(c, color);
    }
}

}

and now my GridView class:

package com.jackson.android.hexagon.grid;

import android.view.*;

import android.content.*;

import android.graphics.*;

public class GridView extends View
{
    private HexGrid grid;

    public GridView(Context context)
    {
        super(context);

        grid = new HexGrid(this, 2, 2);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        grid.draw(canvas, Color.BLUE, Color.RED);
    }
}

Now, the problem is that when I compile launch the activity on my device
the app screen is empty. About four seconds later the screen goes black
and my app quits respinding, leaving me no other choice but to force close
it.

Of course I created an instance of my HexGrid class in my main view.

The first thing I do in these situations is to check the LogCat. Many times
the location in the code will be printed neatly in the log just waiting to be
read and corrected. So I did. The only thing in the LogCat was
“android.jar has stale dependencies”.

This irked me a little. I am sure that my
error was somewhere in the for loops in the HexGrid class, but I am not sure how
to correct them since this is my first time coding a game from scratch
(I have coded a little in libGdx before I decided to first learn Android Canvas
then OpenGL ES, followed by learning direct native OpenGL).

Do you need the code for my Hexagon class too?

Leave a Reply