Hakeem Thomas
Hakeem Thomas

Reputation: 133

How can I set random canvas objects in gridview positions?

I am trying to use 3 canvas objects(drawCircle, drawOval, drawRect) to set in gridview positions to make a bejeweled type game. I have got the random positions using Random.nextInt(bound: 3) but got confused on how to set the positions to canvas objects. I have a canvas class MyCanvas & a CanvasAdapter class for the grid data. My idea is that I create a method in the canvas class that sets a random number in a switch case then draws the objects needed.

  public void randShapes()
    {
        switch(randInt.nextInt(3))
        {
            case 0: pixelPaint.setColor(Color.RED);
                pixelPaint.setStrokeWidth(60);
                myCanvas.drawCircle(0, 0, 1, pixelPaint); return;
            case 1: pixelPaint.setColor(Color.GREEN);
                myCanvas.drawRect(0.5f,0.5f,
                        0.5f, 0.5f, pixelPaint); return;
            case 2: pixelPaint.setColor(Color.BLUE);
                myCanvas.drawRect(0.75f,0.75f,
                        0.5f, 0.5f, pixelPaint); return;
        }
    }  

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        int w = myBitmap.getWidth();
        int h = myBitmap.getHeight();

        randShapes();
    ...
    }

Then in my CanvasAdapter get the positions in an array and reference them back into the canvas class

//CanvasAdapter.java

// Keep all canvas objects in array
    public Integer[] mThumbIds = {
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
            randInt.nextInt(3), randInt.nextInt(3),
    };

//Back to Canvas.java
    int[] shapePosition = CanvasApdapter.mThumbsIds

    public void randShapes()
    {
        for(int i = 0; i < shapePosition.length; i++){
        switch(randInt.nextInt(3))
        {
            case 0: pixelPaint.setColor(Color.RED);
                pixelPaint.setStrokeWidth(60);
                myCanvas.drawCircle(shapePosition.length[i] + 0,shapePosition.length[i] + 0,shapePosition.length[i] + 1, pixelPaint); return;
            case 1: pixelPaint.setColor(Color.GREEN);
                myCanvas.drawRect(shapePosition.length[i] + 0.5f,shapePosition.length[i] + 0.5f,
                        shapePosition.length[i] + 0.5f, shapePosition.length[i] + 0.5f, pixelPaint); return;
            case 2: pixelPaint.setColor(Color.BLUE);
                myCanvas.drawRect(shapePosition.length[i] + 0.75f,shapePosition.length[i] + 0.75f,
                        shapePosition.length[i] + 0.5f, shapePosition.length[i] + 0.5f, pixelPaint); return;
        }
        }
    }  

I'm not sure if I am following the right logic. I am used to unity and c#/c++ programming and android studio makes it hard to follow. I have tried following this tutorial https://www.tutorialspoint.com/android/android_grid_view.htm Thank you all for your help.

Upvotes: 2

Views: 142

Answers (1)

Mike67
Mike67

Reputation: 11342

For the game you're creating, you have defined grid (array) of shapes. Each element in the grid is a random shape. When drawing the grid, just iterate over the rows and columns of the grid and draw the shape based on the array value.

This code should give the basic idea:

//CanvasAdapter.java

// Keep all canvas objects in 2D array, 2 columns, 8 rows
    public Integer[][] mThumbIds = {   // random values
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) },
            { randInt.nextInt(3), randInt.nextInt(3) }
    };

//Back to Canvas.java
    int[][] shapePosition = CanvasApdapter.mThumbsIds
    int shapeheight = 1
    int shapewidth = 1

    public void drawShapes()  // fill in grid
    {
        for(int row = 0; row < shapePosition.length; row++){
            for(int col = 0; col < shapePosition[row].length; col++){
                switch(shapePosition[row][col])
                {
                    case 0: pixelPaint.setColor(Color.RED);
                        pixelPaint.setStrokeWidth(60);
                        myCanvas.drawCircle(row*shapeheight + 0.5f, col*shapewidth + 0, 0.5f, pixelPaint);
                    case 1: pixelPaint.setColor(Color.GREEN);
                        myCanvas.drawRect(row*shapeheight + 0.1f,col*shapewidth + 0.1f, row*shapeheight + 0.9f, col*shapewidth + 0.9f, pixelPaint);
                    case 2: pixelPaint.setColor(Color.BLUE);
                        myCanvas.drawRect(row*shapeheight + 0.1f,col*shapewidth + 0.1f, row*shapeheight + 0.9f, col*shapewidth + 0.9f, pixelPaint);
                }
           }
       }
    } 
    
    /// draw event, redraw grid
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        int w = myBitmap.getWidth();
        int h = myBitmap.getHeight();

        drawShapes();
    ...
    }

Upvotes: 1

Related Questions