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.