Android: error using bitmap to display image using Android studio

I created a 10x10 grid and now I would like to place the player image inside one of the grid squares. for this i think i need to use a bitmap? I will put all my code below. to try to do this, in any case, I created a game class that sets and gets the position x of the playerโ€™s image. Then I created a player class in which I am trying to add an image using Bitmap bitmap = BitmapFactory.decodeResource (getResources (), R.drawable.sokobanplayer1); I keep getting an error in getResources, so I created a method for getting resources in the game, but this does not fix the problem. can 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); } } 
+6
source share
1 answer

Replace the Player class as follows:

 //PLAYER CLASS class Player extends Game { public Player(Context context, int w, int h){ x = 300; y = 300; height = h; width = w; image = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1); } } 
0
source

Source: https://habr.com/ru/post/989185/


All Articles