how to use bitmap to display image in android studio?

I have created a 10×10 grid, and now would like to place an image of the player inside one of the grid squares. to do this I think i have to use bitmap? I will put all my code below. to try and do this anyway I created a game class which sets and gets the x position of the player image. I then created a player class, which is where I try to add the image, using Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1);
i keep getting an error under getResources so i created a get resources method in game, but it didnt fix the problem. could someone please show me how to do this, my code is below.

//DRAW CLASS
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);
    }
}

@Override
public void onDraw(Canvas canvas) {

    drawGrid(canvas);
}
}

//GAME CLASS
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 Bitmap getResources(){
    return image;
}

}


//PLAYER CLASS
public class Player extends Game {

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

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

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


//MAIN ACTIVITY
public class MainActivity extends Activity {
Draw draw;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    draw = new Draw(this);
    draw.setBackgroundColor(Color.BLUE);
    setContentView(draw);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


}


Source: eclipse

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.