Android: how to display an image using bitmap? - java

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);
}
}

Related

Android Studio How to draw a hexagon

I was trying to draw a hexagon on a canvas. But I have not found something that could help me. I wasn't able to do it on my one. My problem is that the hexagon line points aren't drawing.
The green dots are the circles I want to draw and the black lines should be the hexagon when I draw it with lines
My Hexagon class
public class Hexagon extends Rotateables {
public float radius;
public Hexagon(Game game, Vector2 position, float radius) {
super(game, position.x, position.y, 0, 0, 0);
this.radius = radius;
}
public void drawHexagon(Canvas canvas) {
for (int angle = 0; angle < 360;angle += 45) {
setAngle(angle);
Vector2 drawPos = getMovement();
Paint paint;
paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawCircle(drawPos.getX(), drawPos.getY(), radius, paint);
}
}
}
My Rotateables class
public abstract class Rotateables {
protected double x ,y , angle; // angle
protected int w, h; // width and height
// constructor
public Rotateables(double x, double y, double angle, int w, int h) {
this.x = x;
this.y = y;
this.angle = angle;
this.w = w;
this.h = h;
}
// returning all the necessary value of this class
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getAngle() {
return angle;
}
public int getW() {
return w;
}
public int getH() {
return h;
}
// setting the angle
public void setAngle(int aa) {
angle = Math.toRadians(aa);
}
// move toward the angle
// //forward
public void moveForward() {
x += Math.cos(angle);
y += Math.sin(angle);
}
public Vector2 getMovement() {
double x = Math.cos(angle) + this.x;
double y = Math.sin(angle) + this.y;
return new Vector2(x,y);
}
public void moveForward(float speed, boolean drawVertex) {
x += Math.cos(angle)*speed;
y += Math.sin(angle)*speed;
}
//backward
public void moveBackward() {
x -= Math.cos(angle);
y -= Math.sin(angle);
}
public void setAngle(Point p1)
{
double xDiff = x - p1.x;
double yDiff = y - p1.y;
double _angle = Math.toDegrees(Math.atan2(yDiff, xDiff));
setAngle((int)_angle);
}
public static double getAngle(Point p1, Point p2)
{
double xDiff = p2.x - p1.x;
double yDiff = p2.y - p1.y;
return Math.toDegrees(Math.atan2(yDiff, xDiff));
}
}

Changing height of texture makes texture appear above the ground

I want to change the height of my texture to a random height (with a specified range), and I almost figured out how, but the problem I have is that the texture (tower) is now above the ground. I want to stretch the texture while it remains on the same position, but changes height length.
I have a Tower class and a Scrollable class. In the Tower class I generate a random height in the reset method, but the problem is that I don't know what exactly I have to add or write to put the texture on the correct position (so that it isn't above the ground).
Here is the Tower class:
public class Tower extends Scrollable {
private Random r;
// When Tower's constructor is invoked, invoke the super (Scrollable)
// constructor
public Tower(float x, float y, int width, int height, float scrollSpeed) {
super(x, y, width, height, scrollSpeed);
// Initialize a Random object for Random number generation
r = new Random();
}
#Override
public void reset(float newX) {
// Call the reset method in the superclass (Scrollable)
super.reset(newX); // newX
// Change the height to a random number
Random r = new Random();
int low = 0;
int high = 15;
int result = r.nextInt(high-low) + low;
height = result;
}
}
And here's the Scrollable class:
public class Scrollable {
protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledLeft;
public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
position = new Vector2(x, y);
velocity = new Vector2(scrollSpeed, 0);
this.width = width;
this.height = height;
isScrolledLeft = false;
}
public void update(float delta) {
position.add(velocity.cpy().scl(delta));
// If the Scrollable object is no longer visible:
if (position.x + width < 0) {
isScrolledLeft = true;
}
}
// Reset: Should Override in subclass for more specific behavior.
public void reset(float newX) {
position.x = newX;
isScrolledLeft = false;
}
public boolean isScrolledLeft() {
return isScrolledLeft;
}
public float getTailX() {
return position.x + width;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Maybe it's important to know that I have a GameRenderer class which has a drawTowers() method, which is then used in a render() method.
That's my drawTowers() method:
private void drawTowers() {
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight(),
tower1.getWidth(), midPointY - (tower1.getHeight()));
batcher.draw(AssetLoader.texture2, tower2.getX(), tower2.getY() + tower2.getHeight(),
tower2.getWidth(), midPointY - (tower2.getHeight()));
batcher.draw(AssetLoader.texture3, tower3.getX(), tower3.getY() + tower3.getHeight(),
tower3.getWidth(), midPointY - (tower3.getHeight()));
batcher.draw(AssetLoader.texture4, tower4.getX(), tower4.getY() + tower4.getHeight(),
tower4.getWidth(), midPointY - (tower4.getHeight()));
}
You are drawing the tower too high, you need to be adding half the height rather than the whole height.
Here in drawTowers():
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight() / 2, tower1.getWidth(), midPointY - (tower1.getHeight()));
Do the same for the other towers.
This may not be perfectly correct but it shouldn't be far off.

How to resize bitmap in canvas?

I can't find a answer on other questions here and on Google.
The problem is, that the Bitmaps created in GameView are too big on some screens (on my Galaxy S5 they are correct) and they should be scaled with theGameView.getDensity().
GameView class:
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.circle_green);
Circle class:
private int score;
private int y = 0;
private int x = 0;
private int Speed, width, height;
private GameView theGameView;
private Bitmap bmp;
private Random rnd;
public Circle(GameView theGameView, Bitmap bmp) {
this.theGameView = theGameView;
this.bmp = bmp;
this.width = bmp.getWidth();
this.height = bmp.getHeight();
this.score = theGameView.getScore();
rnd = new Random();
x = (int) (30 * theGameView.getDensity() + rnd.nextInt((int) (theGameView.getWidth() - width - 50 * theGameView.getDensity())));
y = (int) (60 * theGameView.getDensity() + rnd.nextInt((int) (theGameView.getHeight() - height - 80 * theGameView.getDensity())));
Speed = (int) (2 * theGameView.getDensity());
}
private void bounceOff() {
if (x + 10 * theGameView.getDensity() > theGameView.getWidth() - width - Speed || x + Speed < 10 * theGameView.getDensity()) {
Speed = -Speed;
}
x = x + Speed;
if (y + 60 * theGameView.getDensity() > theGameView.getHeight() - height - Speed || y + Speed < 60 * theGameView.getDensity()) {
Speed = -Speed;
}
y = y + Speed;
}
public void onDraw(Canvas canvas) {
bounceOff();
if(canvas != null) {
canvas.drawBitmap(bmp, x, y, null);
}
}
public boolean isTouched(float x2, float y2) {
return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
}
It's very easy. Just use canvas.scale
canvas.save(); // Save current canvas params (not only scale)
canvas.scale(scaleX, scaleY);
canvas.restore(); // Restore current canvas params
scale = 1.0f will draw the same visible size bitmap

pathshape not drawing correctly

if i draw two lines using pathshape in shapedrawble that both have the same starting point, they appear to be drawn at different starting points on the screen. when i draw two lines at the same starting point but at different angles they can in the right circumstances appear to be drawn at different starting points as seen below. This is driving me crazy, any help would be appreciated.
public class drawtest extends View {
private ShapeDrawable mDrawable;
Canvas can = null;
float startx;
float starty;
float endx;
float endy;
float width = 0;
float canvasWidth = 0;
float canvasHeight = 0;
private Paint mBitmapPaint;
float height;
Bitmap bm = null;
Path p;
public drawtest(Context context) {
super(context);
endy=0;
endx=0;
startx=0;
starty=0;
width = 0;
height = 0;
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(0, 0, 0, 0);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
canvasHeight = h;
canvasWidth = w;
super.onSizeChanged(w, h, oldw, oldh);
bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
can = new Canvas(bm);
}
public void test(){
setStart(canvasWidth/2, canvasHeight/2);
double r = 125;
double tempx = 236;
double h = startx;
double k = starty;
//(x-h)^2 + (y-k)^2 = r^2
//y = sqrt(r^2 - (x-h)^2) + k
for(int i = 0; i < 2; ++i){
double tempy = Math.sqrt((r*r) - Math.pow((tempx - h),2)) + k;
setEnd((float) tempx, (float) tempy);
draw();
invalidate();
newCanvas();
tempx+=56;
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.drawBitmap(bm, 0, 0, mBitmapPaint);
mDrawable.draw(canvas);
canvas.restore();
}
void newCanvas(){
mDrawable.draw(can);
can.save();
}
public void setStart(float X, float Y){
startx = X;
starty = Y;
}
public void setEnd(float X, float Y){
endx = X;
endy = Y;
width = Math.abs(endx - startx);
height = Math.abs(endy - starty);
}
private void getnewshape() {
p = new Path();
p.moveTo(startx, starty);
p.lineTo(endx, endy);
mDrawable = new ShapeDrawable(new PathShape(p, width, height));
}
public void draw() {
getnewshape();
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.getPaint().setStyle(Paint.Style.STROKE);
mDrawable.setBounds(0, 0, (int) width, (int) height);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
test();
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
}

How to loop sprite when it touches the border (android game development)

please help. I am new to android game development and java. I want my deer to loop or start from 0,0 again when it touches the border of the screen but i have no idea how to code it.
package com.cmanres.bunnyjourney;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Spriteleft {
private static final int BMP_COLUMNS = 3;
private static final int BMP_ROWS = 2;
private int x = 0;
private int y=0;
private int xSpeed = 3;
private Levelunogame gameView;
private Bitmap deer1;
private int width;
private int height;
private int currentFrame=0;
public Spriteleft(Levelunogame gameView, Bitmap deer1) {
this.gameView=gameView;
this.deer1=deer1;
this.width = deer1.getWidth() / BMP_COLUMNS;
this.height = deer1.getHeight() / BMP_ROWS;
}
private void update() {
if (x > gameView.getWidth() - width - xSpeed) {
xSpeed = -3;
}
if (x + xSpeed< 0) {
xSpeed = 3;
}
x = x + xSpeed;
currentFrame = ++currentFrame % BMP_COLUMNS;
}
public void onDraw(Canvas canvas) {
update();
int srcX = currentFrame * width;
int srcY = 1 * height;
Rect src = new Rect(srcX, srcY, srcX + width, srcY + height);
Rect dst = new Rect(x, y, x + width, y + height);
canvas.drawBitmap(deer1, src , dst, null);
}
}
that is my code that i followed on a android mobile game tutorial. can anyone show me how to do it? or give any links for a tutorial? I will appreciate it very much.
Assuming your sprite is positioned at x, y with sprite_width, sprite_height
// Check if your sprite position is outside the screen bounds
if( x < 0 || x > gameView.getWidth() - sprite_width
|| y < 0 || y > gameView.getHeight() - sprite_height ) {
// Set x, y to 0,0
x = 0;
y = 0;
}

Categories