Android: how to display an image using bitmap?

268 views Asked by At

I have created a 10x10 grid for a game, and now i am trying to get the image i have for the player to display inside one of the grid squares. I have a Game, Player and Draw class. in the Game class the x and y position of the player are set, in the Player class is where i set the values for the x and y position and create the bitmap. In the Draw class I create the grid and call it in the onDraw method. I was wondering how do I call the bitmap in the onDraw so that it will display? I dont know if this makes any sense but any help or tips would be good.

public class Game {

Bitmap image;
int x;
int y;
int height;
int width;


public void setX(int x){

    this.x = x;
}

public void setY(int y){

    this.y = y;
}

public int getX()
{
    return x;
}

public int getY(){
    return y;
}

public int getHeight(){
    return height;
}

public int getWidth(){
    return width;
}

}



public class Player extends Game {

public Player(Bitmap res, int w, int h, Context context){

    image = res;
    x = 300;
    y = 300;
    height = 300;
    width = 300;

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}
}



public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();

int rectSide = 1000;

public Draw(Context context) {
    super(context);
    red.setColor(Color.RED);
    green.setColor(Color.GREEN);
    red.setStrokeWidth(8);
    green.setStrokeWidth(8);
}

public void drawGrid(Canvas canvas) {

    int width = canvas.getWidth();
    int height = canvas.getHeight();


    float startX = (width / 2) - (rectSide / 2);
    float stopX = (width / 2) + (rectSide / 2);
    float startY = (height / 2) - (rectSide / 2);
    float stopY = (height / 2) + (rectSide / 2);

    for (int i = 0; i < 1100; i += 100) {
        float newY = startY + i;
        canvas.drawLine(startX, newY, stopX, newY, green);
    }

    for (int i = 0; i < 1100; i += 100) {

        float newX = startX + i;
        canvas.drawLine(newX, startY, newX, stopY, green);
    }
}

public void drawPlayer(Bitmap res, int w, int h, Context context){

    Bitmap image = res;
    int x = 300;
    int y = 300;
    int height = 300;
    int width = 300;

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}

@Override
public void onDraw(Canvas canvas) {

    drawGrid(canvas);
    //canvas.getResources(image, x, y, null);
}
}
0

There are 0 answers