i keep getting error draw to canvas
Error
The error keep happening when I tried to draw to the canvas the player after the user move the joystick
it always writes me "cant lock canvas ,canvas already locked"
i'm kind of beginner in game development and sorry if the question is a little unclear if you need more details just say and I will send you more info
Main class - the main activity
package app.shahardagan.Raven;
import android.graphics.Point;
import android.os.Bundle;
import android.app.Activity;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Main extends Activity {
RelativeLayout layout_joystick;
ImageView image_joystick, image_border;
GameEngine gameEngine;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a Display object to access screen details
Display display = getWindowManager().getDefaultDisplay();
// Load the resolution into a Point object
Point size = new Point();
size.set(display.getWidth(),display.getHeight());
layout_joystick = (RelativeLayout)findViewById(R.id.layout_joystick);
gameEngine = new GameEngine(this,size.x,size.y);
setContentView(gameEngine);
}
#Override
protected void onResume(){
super.onResume();
gameEngine.resume();
}
#Override
protected void onPause(){
super.onPause();
gameEngine.pause();
}
}
GameEngine class - responsible to draw the player and update the game
package app.shahardagan.Raven;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class GameEngine extends SurfaceView implements Runnable {
JoyStickClass js;
// This is our thread
private Thread gameThread = null;
// This is new. We need a SurfaceHolder
// When we use Paint and Canvas in a thread
// We will see it in action in the draw method soon.
private SurfaceHolder ourHolder;
// A boolean which we will set and unset
// when the game is running- or not.
private volatile boolean playing;
// Game is paused at the start
private boolean paused = true;
// A Canvas and a Paint object
private Canvas canvas;
private Paint paint;
// How wide and high is the screen?
private int screenX;
private int screenY;
// This variable tracks the game frame rate
private long fps;
// This is used to help calculate the fps
private long timeThisFrame;
private Context context;
private Player player;
private Drawable playerImage;
public GameEngine(Context context, int x, int y) {
// This calls the default constructor to setup the rest of the object
super(context);
this.context = context;
// Initialize ourHolder and paint objects
ourHolder = getHolder();
paint = new Paint();
// Initialize screenX and screenY because x and y are local
screenX = x;
screenY = y;
prepareLevel();
}
private void prepareLevel(){
player = new Player(context,screenX,screenY);
}
public void PlayerControls(Context context,RelativeLayout layout_joystick, ImageView image_joystick, ImageView image_border){
js = new JoyStickClass(context, layout_joystick, R.drawable.image_button);
js.setStickSize(150, 150);
js.setLayoutSize(500, 500);
js.setLayoutAlpha(150);
js.setStickAlpha(100);
js.setOffset(90);
js.setMinimumDistance(50);
layout_joystick.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
js.drawStick(arg1);
if(arg1.getAction() == MotionEvent.ACTION_DOWN || arg1.getAction() == MotionEvent.ACTION_MOVE) {
int direction = js.get8Direction();
if(direction == JoyStickClass.STICK_UP) {
} else if(direction == JoyStickClass.STICK_UPRIGHT) {
player.setMovementState(Player.UP);
} else if(direction == JoyStickClass.STICK_RIGHT) {
} else if(direction == JoyStickClass.STICK_DOWNRIGHT) {
} else if(direction == JoyStickClass.STICK_DOWN) {
} else if(direction == JoyStickClass.STICK_DOWNLEFT) {
} else if(direction == JoyStickClass.STICK_LEFT) {
} else if(direction == JoyStickClass.STICK_UPLEFT) {
} else if(direction == JoyStickClass.STICK_NONE) {
}
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
});
}
// Runs when the OS calls onPause on BreakoutActivity method
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
Log.e("Error:", "joining thread");
}
}
// Runs when the OS calls onResume on BreakoutActivity method
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
#Override
public void run() {
while (playing) {
// Capture the current time in milliseconds in startFrameTime
long startFrameTime = System.currentTimeMillis();
// Update the frame
// Update the frame
if(!paused){
update();
}
// Draw the frame
draw();
// Calculate the fps this frame
// We can then use the result to
// time animations and more.
timeThisFrame = System.currentTimeMillis() - startFrameTime;
if (timeThisFrame >= 1) {
fps = 1000 / timeThisFrame;
}
}
}
private void draw() {
// Make sure our drawing surface is valid or game will crash
if (ourHolder.getSurface().isValid()) {
// Lock the canvas ready to draw
canvas = ourHolder.lockCanvas();
// Draw the background color
canvas.drawColor(Color.argb(255, 26, 128, 182));
// Draw everything to the screen
// Choose the brush color for drawing
paint.setColor(Color.argb(255, 255, 255, 255));
canvas.drawBitmap(player.getBitmap(),player.getX(),screenY - player.getHeight(),paint);
}
}
private void update() {
player.update(fps);
}
}
Player class - the player class methods
package app.shahardagan.Raven;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
public class Player {
// RectF is an object that holds four coordinates - just what we need
private RectF rect;
private Bitmap bitmap;
// How long will our paddle will be
private float length;
private float height;
// X is the far left of the rectangle which forms our paddle
private float x;
private float y;
// Which ways can the player move
final int STOPPED = 0;
public static final int UP = 1;
public static final int UPRIGHT = 2;
public static final int RIGHT = 3;
public static final int DOWNRIGHT = 4;
public static final int DOWN = 5;
public static final int DOWNLEFT = 6;
public static final int LEFT = 7;
public static final int UPLEFT = 8;
// Is the paddle moving and in which direction
private int playerMoving = STOPPED;
private int playerSpeed;
public Player(Context context,int screenX, int screenY){
rect = new RectF();
length =screenX/10;
height = screenX/10;
x = screenX;
y= 0;
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.eagle_fly);
bitmap = Bitmap.createScaledBitmap(bitmap,(int)length,(int)height,false);
playerSpeed = 350;
}
public RectF getRect(){
return rect;
}
public Bitmap getBitmap(){
return bitmap;
}
public float getX(){ return x; }
public float getHeight(){return height;}
private float getLength(){return length;}
public void setMovementState(int state){
playerMoving = state;
}
void update(long fps){
if(playerMoving == LEFT){
x = x - playerSpeed / fps;
}
if(playerMoving == RIGHT){
x = x + playerSpeed / fps;
}
rect.top = y;
rect.bottom = y+ height;
rect.left = x;
rect.right = x + length;
}
}
Joy stick class
package app.shahardagan.Raven;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
public class JoyStickClass {
public static final int STICK_NONE = 0;
public static final int STICK_UP = 1;
public static final int STICK_UPRIGHT = 2;
public static final int STICK_RIGHT = 3;
public static final int STICK_DOWNRIGHT = 4;
public static final int STICK_DOWN = 5;
public static final int STICK_DOWNLEFT = 6;
public static final int STICK_LEFT = 7;
public static final int STICK_UPLEFT = 8;
private int STICK_ALPHA = 200;
private int LAYOUT_ALPHA = 200;
private int OFFSET = 0;
private Context mContext;
private ViewGroup mLayout;
private LayoutParams params;
private int stick_width, stick_height;
private int position_x = 0, position_y = 0, min_distance = 0;
private float distance = 0, angle = 0;
private DrawCanvas draw;
private Paint paint;
private Bitmap stick;
private boolean touch_state = false;
public JoyStickClass (Context context, ViewGroup layout, int stick_res_id) {
mContext = context;
stick = BitmapFactory.decodeResource(mContext.getResources(),
stick_res_id);
stick_width = stick.getWidth();
stick_height = stick.getHeight();
draw = new DrawCanvas(mContext);
paint = new Paint();
mLayout = layout;
params = mLayout.getLayoutParams();
}
public void drawStick(MotionEvent arg1) {
position_x = (int) (arg1.getX() - (params.width / 2));
position_y = (int) (arg1.getY() - (params.height / 2));
distance = (float) Math.sqrt(Math.pow(position_x, 2) + Math.pow(position_y, 2));
angle = (float) cal_angle(position_x, position_y);
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
if(distance <= (params.width / 2) - OFFSET) {
draw.position(arg1.getX(), arg1.getY());
draw();
touch_state = true;
}
} else if(arg1.getAction() == MotionEvent.ACTION_MOVE && touch_state) {
if(distance <= (params.width / 2) - OFFSET) {
draw.position(arg1.getX(), arg1.getY());
draw();
} else if(distance > (params.width / 2) - OFFSET){
float x = (float) (Math.cos(Math.toRadians(cal_angle(position_x, position_y))) * ((params.width / 2) - OFFSET));
float y = (float) (Math.sin(Math.toRadians(cal_angle(position_x, position_y))) * ((params.height / 2) - OFFSET));
x += (params.width / 2);
y += (params.height / 2);
draw.position(x, y);
draw();
} else {
mLayout.removeView(draw);
}
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
mLayout.removeView(draw);
touch_state = false;
}
}
public int[] getPosition() {
if(distance > min_distance && touch_state) {
return new int[] { position_x, position_y };
}
return new int[] { 0, 0 };
}
public int getX() {
if(distance > min_distance && touch_state) {
return position_x;
}
return 0;
}
public int getY() {
if(distance > min_distance && touch_state) {
return position_y;
}
return 0;
}
public float getAngle() {
if(distance > min_distance && touch_state) {
return angle;
}
return 0;
}
public float getDistance() {
if(distance > min_distance && touch_state) {
return distance;
}
return 0;
}
public void setMinimumDistance(int minDistance) {
min_distance = minDistance;
}
public int getMinimumDistance() {
return min_distance;
}
public int get8Direction() {
if(distance > min_distance && touch_state) {
if(angle >= 247.5 && angle < 292.5 ) {
return STICK_UP;
} else if(angle >= 292.5 && angle < 337.5 ) {
return STICK_UPRIGHT;
} else if(angle >= 337.5 || angle < 22.5 ) {
return STICK_RIGHT;
} else if(angle >= 22.5 && angle < 67.5 ) {
return STICK_DOWNRIGHT;
} else if(angle >= 67.5 && angle < 112.5 ) {
return STICK_DOWN;
} else if(angle >= 112.5 && angle < 157.5 ) {
return STICK_DOWNLEFT;
} else if(angle >= 157.5 && angle < 202.5 ) {
return STICK_LEFT;
} else if(angle >= 202.5 && angle < 247.5 ) {
return STICK_UPLEFT;
}
} else if(distance <= min_distance && touch_state) {
return STICK_NONE;
}
return 0;
}
public int get4Direction() {
if(distance > min_distance && touch_state) {
if(angle >= 225 && angle < 315 ) {
return STICK_UP;
} else if(angle >= 315 || angle < 45 ) {
return STICK_RIGHT;
} else if(angle >= 45 && angle < 135 ) {
return STICK_DOWN;
} else if(angle >= 135 && angle < 225 ) {
return STICK_LEFT;
}
} else if(distance <= min_distance && touch_state) {
return STICK_NONE;
}
return 0;
}
public void setOffset(int offset) {
OFFSET = offset;
}
public int getOffset() {
return OFFSET;
}
public void setStickAlpha(int alpha) {
STICK_ALPHA = alpha;
paint.setAlpha(alpha);
}
public int getStickAlpha() {
return STICK_ALPHA;
}
public void setLayoutAlpha(int alpha) {
LAYOUT_ALPHA = alpha;
mLayout.getBackground().setAlpha(alpha);
}
public int getLayoutAlpha() {
return LAYOUT_ALPHA;
}
public void setStickSize(int width, int height) {
stick = Bitmap.createScaledBitmap(stick, width, height, false);
stick_width = stick.getWidth();
stick_height = stick.getHeight();
}
public void setStickWidth(int width) {
stick = Bitmap.createScaledBitmap(stick, width, stick_height, false);
stick_width = stick.getWidth();
}
public void setStickHeight(int height) {
stick = Bitmap.createScaledBitmap(stick, stick_width, height, false);
stick_height = stick.getHeight();
}
public int getStickWidth() {
return stick_width;
}
public int getStickHeight() {
return stick_height;
}
public void setLayoutSize(int width, int height) {
params.width = width;
params.height = height;
}
public int getLayoutWidth() {
return params.width;
}
public int getLayoutHeight() {
return params.height;
}
private double cal_angle(float x, float y) {
if(x >= 0 && y >= 0)
return Math.toDegrees(Math.atan(y / x));
else if(x < 0 && y >= 0)
return Math.toDegrees(Math.atan(y / x)) + 180;
else if(x < 0 && y < 0)
return Math.toDegrees(Math.atan(y / x)) + 180;
else if(x >= 0 && y < 0)
return Math.toDegrees(Math.atan(y / x)) + 360;
return 0;
}
private void draw() {
try {
mLayout.removeView(draw);
} catch (Exception e) { }
mLayout.addView(draw);
}
private class DrawCanvas extends View{
float x, y;
private DrawCanvas(Context mContext) {
super(mContext);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(stick, x, y, paint);
}
private void position(float pos_x, float pos_y) {
x = pos_x - (stick_width / 2);
y = pos_y - (stick_height / 2);
}
}
}
In your draw() method, you're locking the Canvas, but not unlocking the Canvas after you finish drawing. So, when you re-enter your draw method, the Canvas is still locked from the previous call.
You likely need to add unlockCanvasAndPost(Canvas canvas) after you finish drawing. See the full docs here.
Related
I have been working at it for some time and looking it up online however can't find anything.
I'm trying to work out why the Log I created under
Player.Update() is always returning 0 whereas the Log in my Setter is reporting the right values.
The relevant code should be below.
MainActivity
package com.Frenchie.AnimatedSprite;
import ...
public class MainActivity extends Activity implements SensorEventListener {
//Accelerometer
private SensorManager senSensorManager;
private Sensor senAccelerometer;
private static int DIRECTION_STATIONARY = 0;
private static int DIRECTION_DOWN = 1;
private static int DIRECTION_LEFT = 2;
private static int DIRECTION_RIGHT = 3;
private static int DIRECTION_UP = 4;
Player player;
GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView = new GameView(this);
player = new Player();
setContentView(gameView);
//Accelerometer
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_FASTEST);
}
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor mySensor = sensorEvent.sensor;
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if (sensorEvent.values[0] < -1){
player.setDirection(DIRECTION_UP);
//Log.d("onSensorChanged", ""+ player.getDirection());
}
else if (sensorEvent.values[0] > 1){
player.setDirection(DIRECTION_DOWN);
}
else if (sensorEvent.values[1] < -1){
player.setDirection(DIRECTION_LEFT);
}
else if (sensorEvent.values[1] > 1){
player.setDirection(DIRECTION_RIGHT);
}
else{
player.setDirection(DIRECTION_STATIONARY);
}
//Log.d("Player Update", "X:" +sensorEvent.values[0]+ " Y:" +sensorEvent.values[1]+ " Z:" +sensorEvent.values[2] + " Direction: " + direction);
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
Player.java
package com.Frenchie.AnimatedSprite;
import ...
public class Player {
private Bitmap sprite;
private int x, y, speed;
//Animation Variables
private static final int SPRITE_ROWS = 4;
private static final int SPRITE_COLUMNS = 4;
private int currentFrame, width, height, srcY, srcX, direction;
Rect src, dst;
//Static Directions
private static int DIRECTION_STATIONARY = 0;
private static int DIRECTION_DOWN = 1;
private static int DIRECTION_LEFT = 2;
private static int DIRECTION_RIGHT = 3;
private static int DIRECTION_UP = 4;
GameView gameView;
public Player (Context context, GameView gameView){
this.gameView = gameView;
x = 100;
y = 500;
speed = 30;
sprite = BitmapFactory.decodeResource(context.getResources(), R.drawable.player);
width = sprite.getWidth() / SPRITE_COLUMNS;
height = sprite.getHeight() / SPRITE_ROWS;
}
public Player() {
}
public void Update() {
Log.d("Player | Update", "" + direction);
if (direction == DIRECTION_UP){
y -= speed;
}
else if (direction == DIRECTION_DOWN){
y += speed;
}
else if (direction == DIRECTION_RIGHT){
x += speed;
}
else if (direction == DIRECTION_LEFT){
x -= speed;
}
else if (direction == 0){
}
}
public void UpdateAnim(){
currentFrame = ++currentFrame % SPRITE_COLUMNS;
srcX = currentFrame * width;
if (direction == DIRECTION_RIGHT){
srcY = 2 * height;
}
else if (direction == DIRECTION_LEFT){
srcY = 1 * height;
}
if (direction == DIRECTION_DOWN){
srcY = 0 * height;
}
else if (direction == DIRECTION_UP){
srcY = 3 * height;
}
src = new Rect(srcX, srcY, srcX + width, srcY + height);
dst = new Rect(x, y, x + width, y + height);
}
public Rect getSrc(){
return src;
}
public Rect getDst(){
return dst;
}
public Bitmap getSprite() {
return sprite;
}
public void setDirection(int mainDirection) {
this.direction = mainDirection;
Log.d("Setter", "" + direction);
}
}
GameView
package com.Frenchie.AnimatedSprite;
import ...
public class GameView extends SurfaceView implements Runnable {
private Canvas canvas;
private SurfaceHolder surfaceHolder;
private Thread thread;
Player player;
public GameView(Context context) {
super(context);
player = new Player(context, this);
surfaceHolder = getHolder();
thread = new Thread(this);
thread.start();
}
#Override
public void run() {
while (true) {
Update();
DrawCanvas();
}
}
private void Update() {
player.Update();
player.UpdateAnim();
Control();
}
private void DrawCanvas() {
canvas = surfaceHolder.lockCanvas();
if (surfaceHolder.getSurface().isValid()) {
canvas.drawColor(Color.MAGENTA);
canvas.drawBitmap(player.getSprite(), player.getSrc(), player.getDst(), null);
surfaceHolder.unlockCanvasAndPost(canvas);
} else {
Log.d("Run", "Surface Invalid");
}
}
private void Control() {
try {
thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
If you need any more information let me know.
I'm recreating Asteroids, the spaceship can move forward and backwards and change it's rotation. When I for example move forward, the velocity keeps increasing until I release the forward key. But it also stops increasing the velocity when I for example hit the rotate left key. I don't understand why it does this, and I want to be able to increase the rotation degrees while increasing or decreasing my velocity (i.e. pressing UP or DOWN arrow). It also stops moving forward or rotating when I shoot (i.e. space bar).
Also, is my code readable and the aproach OO?
Asteroids:
import javax.swing.*;
public class Asteroids {
public static void createAndShowGui() {
GamePanel gamePanel = new GamePanel();
JFrame frame = new JFrame("Asteroids");
frame.getContentPane().add(gamePanel);
frame.pack();
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocation(2000, 50);
gamePanel.requestFocusInWindow();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
GamePanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
public class GamePanel extends JPanel implements ActionListener {
private final int WIDTH = 1600;
private final int HEIGHT = 900;
private ArrayList<Rock> rocks;
private ArrayList<Bullet> bullets;
private SpaceShip spaceShip;
private boolean keyHeld;
private int keyCode;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setUp();
}
public void setUp() {
Timer animationTimer = new Timer(15, this);
spaceShip = new SpaceShip(WIDTH, HEIGHT);
bullets = new ArrayList<>();
rocks = new ArrayList<>();
addKeyListener(new KeyListener());
for (int i = 0; i < 12; i++) {
rocks.add(new Rock(WIDTH, HEIGHT));
}
animationTimer.start();
}
class KeyListener extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
keyHeld = true;
}
#Override
public void keyReleased(KeyEvent e) {
keyHeld = false;
}
}
#Override
public void actionPerformed(ActionEvent e) {
checkRockCollisions();
for (int i = 0; i < bullets.size(); i++)
checkBulletOffScreen(bullets.get(i));
spaceShip.checkForCollisionWithFrame(WIDTH, HEIGHT);
moveObjects();
checkForPressedKeys();
repaint();
}
public void moveObjects() {
for (Rock rock : rocks)
rock.moveForward();
for (Bullet bullet : bullets)
bullet.moveForward();
spaceShip.moveSpaceShip();
}
public void checkRockCollisions() {
for (int i = 0; i < rocks.size(); i++) {
Rock rock = rocks.get(i);
rocks.stream().filter(rockToCheck -> !rock.equals(rockToCheck)).forEach(rock::checkRockCollision);
for (int j = 0; j < bullets.size(); j++) {
Bullet bullet = bullets.get(j);
if (rock.checkBulletCollision(bullet)) {
rocks.remove(rock);
bullets.remove(bullet);
}
}
if (rock.checkSpaceShipCollision(spaceShip))
resetSpaceShip();
rock.checkFrameCollision(WIDTH, HEIGHT);
}
}
public void checkBulletOffScreen(Bullet bullet) {
if (bullet.getxPos() + bullet.getBulletWidth() < 0 || bullet.getxPos() > WIDTH || bullet.getyPos() + bullet.getBulletHeight() < 0 || bullet.getyPos() > HEIGHT)
bullets.remove(bullet);
}
public void resetSpaceShip() {
spaceShip = new SpaceShip(WIDTH, HEIGHT);
}
public void checkForPressedKeys() {
if (keyHeld) {
switch (keyCode) {
case KeyEvent.VK_RIGHT:
spaceShip.increaseRotationDegree();
break;
case KeyEvent.VK_LEFT:
spaceShip.decreaseRotationDegree();
break;
case KeyEvent.VK_UP:
spaceShip.increaseForwardVelocity();
break;
case KeyEvent.VK_DOWN:
spaceShip.decreaseForwardVelocity();
break;
case KeyEvent.VK_SPACE:
bullets.add(new Bullet(spaceShip.getxPos() + spaceShip.getSpaceShipRadius(), spaceShip.getyPos() + spaceShip.getSpaceShipRadius(), spaceShip.getRotationDegree()));
keyHeld = false;
break;
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
RenderingHints mainRenderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(mainRenderingHints);
for (Rock rock : rocks)
rock.display(g2d);
for (Bullet bullet : bullets)
bullet.display(g2d);
spaceShip.display(g2d);
g2d.dispose();
}
}
SpaceShip.java
import java.awt.*;
public class SpaceShip {
private double xPos;
private double yPos;
private double xVelocity;
private double yVelocity;
private int spaceShipDiameter;
private int rotationDegree;
private int spaceShipRadius;
public SpaceShip(final int WIDTH, final int HEIGHT) {
spaceShipDiameter = 30;
spaceShipRadius = spaceShipDiameter / 2;
xPos = WIDTH / 2 - spaceShipRadius;
yPos = HEIGHT / 2 - spaceShipRadius;
rotationDegree = 0;
}
public int getSpaceShipRadius() {
return spaceShipRadius;
}
public double getxPos() {
return xPos;
}
public double getyPos() {
return yPos;
}
public int getRotationDegree() {
return rotationDegree;
}
public void increaseForwardVelocity() {
xVelocity += 0.04 * Math.sin(Math.toRadians(this.rotationDegree));
yVelocity += 0.04 * -Math.cos(Math.toRadians(this.rotationDegree));
}
public void decreaseForwardVelocity() {
xVelocity -= 0.04 * Math.sin(Math.toRadians(this.rotationDegree));
yVelocity -= 0.04 * -Math.cos(Math.toRadians(this.rotationDegree));
}
public void moveSpaceShip() {
this.xPos += xVelocity;
this.yPos += yVelocity;
}
public void increaseRotationDegree() {
if (rotationDegree >= 350)
rotationDegree = 0;
else
rotationDegree+=10;
}
public void decreaseRotationDegree() {
if (rotationDegree <= 0)
rotationDegree = 350;
else
rotationDegree-=10;
}
public void checkForCollisionWithFrame(final int WIDTH, final int HEIGHT) {
if (xPos + spaceShipDiameter < 0)
xPos = WIDTH;
else if (xPos > WIDTH)
xPos = 0 - spaceShipDiameter;
else if (yPos + spaceShipDiameter < 0)
yPos = HEIGHT;
else if (yPos > HEIGHT)
yPos = 0 - spaceShipDiameter;
}
public void display(Graphics2D g2d) {
g2d.setColor(Color.BLACK);
g2d.rotate(Math.toRadians(rotationDegree), xPos + spaceShipRadius, yPos + spaceShipRadius);
g2d.fillOval((int) xPos, (int) yPos, spaceShipDiameter, spaceShipDiameter);
g2d.setColor(Color.YELLOW);
g2d.drawLine((int) xPos + spaceShipRadius, (int) yPos , (int) xPos + spaceShipRadius, (int) yPos + spaceShipRadius);
}
}
Rock.java
import java.awt.*;
import java.util.Random;
public class Rock {
private int xPos;
private int yPos;
private int rockDiameter;
private int xVelocity;
private int yVelocity;
private int rockRadius;
private Color rockColor;
public Rock(int WIDTH, int HEIGHT) {
Random r = new Random();
rockDiameter = r.nextInt(40) + 30;
rockRadius = rockDiameter / 2;
xPos = r.nextInt(WIDTH - rockDiameter);
yPos = r.nextInt(HEIGHT - rockDiameter);
xVelocity = r.nextInt(6) - 3;
yVelocity = r.nextInt(6) - 3;
rockColor = new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255));
}
public void moveForward() {
xPos += xVelocity;
yPos += yVelocity;
}
public void checkRockCollision(Rock rock) {
if (calculateDistances(rock.xPos, rock.yPos, rock.rockRadius))
switchVelocities(rock);
}
public void switchVelocities(Rock rock) {
int tempXVelocity = this.xVelocity;
int tempYVelocity = this.yVelocity;
this.xVelocity = rock.xVelocity;
this.yVelocity = rock.yVelocity;
rock.xVelocity = tempXVelocity;
rock.yVelocity = tempYVelocity;
moveForward();
rock.moveForward();
}
public boolean checkBulletCollision(Bullet bullet) {
return calculateDistances(bullet.getxPos(), bullet.getyPos(), bullet.getBulletRadius());
}
public boolean checkSpaceShipCollision(SpaceShip spaceShip) {
return calculateDistances(spaceShip.getxPos(), spaceShip.getyPos(), spaceShip.getSpaceShipRadius());
}
public boolean calculateDistances(double objectxPos, double objectyPos, int objectRadius) {
int radiusOfBoth = objectRadius + rockRadius;
double horDistance = Math.abs((objectxPos + objectRadius) - (xPos + rockRadius));
double verDistance = Math.abs((objectyPos + objectRadius) - (yPos + rockRadius));
double diagDistance = Math.sqrt(Math.pow(horDistance, 2) + Math.pow(verDistance, 2));
return diagDistance <= radiusOfBoth;
}
public void checkFrameCollision(final int WIDTH, final int HEIGHT) {
if (xPos < 0) {
xVelocity *= -1;
xPos = 0;
} else if (xPos + rockDiameter > WIDTH) {
xVelocity *= -1;
xPos = WIDTH - rockDiameter;
} else if (yPos < 0) {
yVelocity *= -1;
yPos = 0;
} else if (yPos + rockDiameter > HEIGHT) {
yVelocity *= -1;
yPos = HEIGHT - rockDiameter;
}
}
public void display(Graphics2D g2d) {
g2d.setColor(rockColor);
g2d.fillOval(xPos, yPos, rockDiameter, rockDiameter);
}
}
Bullet.java
import java.awt.*;
public class Bullet {
private double xPos;
private double yPos;
private int bulletWidth;
private int bulletHeight;
private double xVelocity;
private double yVelocity;
private int bulletRadius;
public Bullet(double startingXPos, double startingYPos, int rotationDegree) {
bulletHeight = 10;
bulletWidth = 10;
bulletRadius = bulletWidth / 2;
xPos = startingXPos - bulletRadius;
yPos = startingYPos - bulletRadius;
xVelocity = 5 * Math.sin(Math.toRadians(rotationDegree));
yVelocity = 5 * -Math.cos(Math.toRadians(rotationDegree));
}
public void moveForward() {
this.xPos += xVelocity;
this.yPos += yVelocity;
}
public double getxPos() {
return xPos;
}
public double getyPos() {
return yPos;
}
public int getBulletWidth() {
return bulletWidth;
}
public int getBulletHeight() {
return bulletHeight;
}
public int getBulletRadius() {
return bulletRadius;
}
public void display(Graphics2D g2d) {
g2d.setColor(Color.GREEN);
g2d.fillOval((int) xPos, (int) yPos, bulletWidth, bulletHeight);
}
}
You're only keeping track of the most recent key pressed. Instead, you need to keep track of every key being pressed. One way to do that is by using a boolean variable for each key. In your keyPressed() function, set the appropriate boolean to true, and in the keyReleased() function, set it to false. Then in your game loop, just check which booleans are true and take the appropriate action.
I'm creating a game like zType
and I have a problem in validating the input. As you can see in the picture below it select two words. How can I fix this problem, please help.
This is My Game class
import java.awt.Canvas;
import java.awt.event.KeyListener;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable, KeyListener
{
public static final int WIDTH = 640;
public static final int HEIGHT = 680;
public final String TITLE = "Game";
private boolean running = false;
private Thread thread;
private BufferedImage spriteSheet = null;
private BufferedImage background = null;
public int level = 1;
public int fps = 0;
public int score = 0;
private int enemy_count = 15;
private int enemy_killed = 0;
private int enemy_notkilled = 0;
private boolean target = false;
public String t = "";
private Controller c;
private Textures tex;
private LinkedList<Enemy> e = new LinkedList<Enemy>();
private Enemy enemy;
public void init()
{
requestFocus();
try
{
spriteSheet = ImageIO.read(getClass().getResource("/sprite_sheet.png"));
background = ImageIO.read(getClass().getResource("/background.png"));
}
catch(IOException e)
{
e.printStackTrace();
}
tex = new Textures(this);
c = new Controller(tex, this);
addKeyListener(this);
c.createEnemy(enemy_count);
e = c.getEnemy();
}
private synchronized void start()
{
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
private synchronized void stop()
{
if(!running)
return;
running = false;
try
{
thread.join();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
System.exit(1);
}
public void run()
{
init();
long lastTime = System.nanoTime();
final double amountOfTicks= 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while(running)
{
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if(delta >= 1)
{
update();
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000)
{
timer += 1000;
fps = frames;
frames = 0;
}
}
stop();
}
private void update()
{
c.update();
if(enemy_killed + enemy_notkilled >= enemy_count)
{
e.clear();
t = "";
level++;
enemy_killed = 0;
enemy_notkilled = 0;
c.createEnemy(enemy_count);
}
}
private void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(background, 0, 0, null);
c.render(g);
g.dispose();
bs.show();
}
public void keyPressed(KeyEvent ek)
{
int key = ek.getKeyCode();
char character = Character.toLowerCase(ek.getKeyChar());
boolean result = isValid(key);
if(result && !target)
{
for(int i = 0; i < e.size(); i++)
if(e.get(i).getFirstLetter() == character && e.get(i).getOnScreen())
{
enemy = e.get(i);
t = enemy.getText();
if(enemy.getCurrentIndex() == 0)
enemy.addCurrentIndex();
target = true;
System.out.println("---"+enemy.text);
break;
}
}
else if(result && t.charAt(enemy.getCurrentIndex()) == character)
enemy.addCurrentIndex();
}
public void keyReleased(KeyEvent ek){ }
public void keyTyped(KeyEvent ek){ }
public static void main(String[] args)
{
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH, HEIGHT));
game.setMaximumSize(new Dimension(WIDTH, HEIGHT));
game.setMinimumSize(new Dimension(WIDTH, HEIGHT));
JFrame frame = new JFrame(game.TITLE);
frame.add(game);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.start();
}
public boolean isValid(int key)
{
return key >= 65 && key <= 90 ? true : false;
}
public BufferedImage getSpriteSheet()
{
return spriteSheet;
}
public int getEnemy_count()
{
return enemy_count;
}
public void setEnemy_count(int enemy_count)
{
this.enemy_count = enemy_count;
}
public int getEnemy_killed()
{
return enemy_killed;
}
public void setEnemy_killed(int enemy_killed)
{
this.enemy_killed = enemy_killed;
}
public int getEnemy_notkilled()
{
return enemy_notkilled;
}
public void setEnemy_notkilled(int enemy_notkilled)
{
this.enemy_notkilled = enemy_notkilled;
}
public void addScore(int k)
{
score += k;
}
public void falseTarget()
{
target = false;
}
public String getT()
{
return t;
}
public void setT(String k)
{
t = k;
}
}
This is my Enemy Class
import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
public class Enemy
{
private double x ,y;
public String text;
private char firstLetter;
private AttributedString as;
private Controller c;
private Textures tex;
private Game game;
private int currentIndex = 0;
private int textLength = 0;
private double speed = 0.0;
private int stringWidth = 0;
private boolean onScreen = false;
public Enemy(double x, double y, double speed, Textures tex, Controller c, Game game, String text, int stringWidth)
{
this.x = x;
this.y = y;
this.tex = tex;
this.text = text;
this.game = game;
this.c = c;
this.speed = speed;
firstLetter = this.text.charAt(0);
textLength = this.text.length();
this.stringWidth = stringWidth;
}
public void update()
{
y += speed;
if(y >= 0)
onScreen = true;
if(currentIndex >= textLength)
{
game.addScore(5);
game.setT("");
c.removeEnemy(this);
game.setEnemy_killed(game.getEnemy_killed() + 1);
game.falseTarget();
}
if(y >= Game.HEIGHT - 50)
{
//game.decreaseHealth();
game.setT("");
c.removeEnemy(this);
game.setEnemy_notkilled(game.getEnemy_notkilled() + 1);
game.falseTarget();
game.t ="";
}
}
public void render(Graphics g)
{
g.drawImage(tex.enemy, (int)x, (int)y, null);
as = new AttributedString(text);
if(currentIndex >= 1)
as.addAttribute(TextAttribute.FOREGROUND, Color.WHITE, 0, currentIndex);
as.addAttribute(TextAttribute.FONT, new Font("Consolas", Font.BOLD, 12), 0, text.length());
g.drawString(as.getIterator(), (int)x + getAdd(stringWidth), (int)y + 13);
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
public String getText()
{
return text;
}
public char getFirstLetter()
{
return firstLetter;
}
public void addCurrentIndex()
{
currentIndex++;
}
public int getCurrentIndex()
{
return currentIndex;
}
public int getTextLength()
{
return textLength;
}
public int getAdd(int width)
{
return (96 / 2) - (width / 2);
}
public boolean getOnScreen()
{
return onScreen;
}
}
The problem is that you're calling falseTarget() whenever an enemy moves out of the screen. In your example, you assigned target = true when you pressed the 'o' (for the enemy named "owe"), but then a different enemy (maybe "jet", maybe an enemy that is no longer visible) finished "dropping" out of the screen (y >= Game.HEIGHT - 50), so you called falseTarget() - and thus, when you pressed 'i' you started a new enemy ("ice").
What you want to do is call falseTarget() in this case only if the enemy that dropped out of the screen is the one that's currently being targeted:
if(y >= Game.HEIGHT - 50)
{
//game.decreaseHealth();
//game.setT(""); // <-- Commented this line out
c.removeEnemy(this);
game.setEnemy_notkilled(game.getEnemy_notkilled() + 1);
if(game.getT().equals(text)) // <-- Added this if before calling falseTarget()
{ // and clearing the game's current enemy text
game.falseTarget();
game.t ="";
}
}
2 part question here. I'm having some trouble here with my for loops. The basic idea of my code is that whenever checkX and checkY are called they compare the x of the touch and the x of the player then if the players x is less than the touchx then it increments the player x by 1 until the player x equals touch x. Its the exact same thing for checky. Everything works fine, the problem is that it never stops. The player just keeps moving on the exact same path.
Heres the player class
package com.gametest;
import java.util.concurrent.CopyOnWriteArrayList;
import com.gametest.GameSurfaceView.MyView;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Player {
int ID;
static int x, y, width, height, widthEnd, currentFrame, boundsX, boundsY,
dirTimer, dir, simx, simy;
static Bitmap currentSprite, playerSprite, deathSprite;
static MyView mv;
public Player(MyView v, Bitmap orb, Bitmap explosion, int screenWidth,
int screenHeight) {
// TODO Auto-generated constructor stub
playerSprite = orb;
deathSprite = explosion;
currentSprite = playerSprite;
mv = v;
height = playerSprite.getHeight();
width = playerSprite.getWidth() / 3;
currentFrame = 1;
}
public static void sprite_draw(Canvas canvas,
CopyOnWriteArrayList<Sprite> copy) {
// TODO Auto-generated method stub
x = (mv.getWidth() / 2) - 50;
y = (mv.getHeight() / 2) - 50;
currentFrame = currentFrame + 1;
if (currentFrame > 3) {
currentFrame = 1;
}
widthEnd = (currentFrame * width) + width;
boundsX = x + width;
boundsY = y + height;
Rect src = new Rect(currentFrame * width, 0, widthEnd, height);
Rect dst = new Rect(x, y, boundsX, boundsY);
canvas.drawBitmap(currentSprite, src, dst, null);
for (Sprite s : copy) {
if (s.x + 50 > x && s.x + 50 < boundsX && s.y + 50 > y
&& s.y + 50 < boundsY) {
GameSurfaceView.damagePlayer();
};
}
}
public void checkX(Integer touchx) {
if (touchx > x) {
for (int newx = x; newx < touchx; newx++) {
GameSurfaceView.setDirection(1, 0);
try {
mv.t.sleep (10);
} catch (InterruptedException e) {
}
}
}
if (touchx < x ) {
for (int newx = x; newx > touchx; newx--) {
GameSurfaceView.setDirection(-1, 0);
try {
mv.t.sleep (10);
} catch (InterruptedException e) {
}
}
}
}
public void checkY(int touchy) {
if (touchy > y) {
for (int newy = y; newy < touchy; newy++) {
GameSurfaceView.setDirection(0, 1);
try {
mv.t.sleep (10);
} catch (InterruptedException e) {
}
}
}
if (touchy < y) {
for (int newy = y; newy > touchy; newy--) {
GameSurfaceView.setDirection(0, -1);
try {
mv.t.sleep (10);
} catch (InterruptedException e) {
}
}
}
}
}
And in case its needed here is the surface view class
package com.gametest;
import java.util.concurrent.CopyOnWriteArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class GameSurfaceView extends Activity implements OnTouchListener {
double ran;
int touchX, touchY, screenWidth, screenHeight, objX, objY;
static boolean canUpdate;
static int enemyCount, score, playerHealth, test1, test2;
static MyView v;
static Bitmap orb, orb2, explosion;
static CopyOnWriteArrayList<Sprite> copy = new CopyOnWriteArrayList<Sprite>();
static String hpString;
static Player player;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
v = new MyView(this);
v.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent me) {
touchX = (int) me.getX();
touchY = (int) me.getY();
for (Sprite sprite : copy) {
sprite.checkTouch(touchX, touchY);
player.checkY(touchY);
player.checkX(touchX);
}
return true;
}
});
canUpdate = true;
screenWidth = v.getWidth();
screenHeight = v.getHeight();
playerHealth = 250;
hpString = "Health " + playerHealth;
ran = 0;
score = 0;
test1 = 5000;
test2 = 5000;
orb = BitmapFactory.decodeResource(getResources(), R.drawable.blue_orb);
orb2 = BitmapFactory.decodeResource(getResources(), R.drawable.red_orb);
explosion = BitmapFactory.decodeResource(getResources(), R.drawable.explosion);
player = new Player(v, orb2, explosion, screenWidth, screenHeight);
createEnemies();
setContentView(v);
}
private void createEnemies() {
if (enemyCount < 5) {
screenWidth = v.getWidth();
screenHeight = v.getHeight();
int listLength = copy.size();
copy.add(new Sprite(v, orb, explosion, screenWidth, screenHeight, listLength));
enemyCount = enemyCount + 1;
}
}
public static void checkECount(int id) {
canUpdate = false;
copy.remove(id);
enemyCount = enemyCount - 1;
int index = 0;
for(Sprite s : copy) {
s.ID = index;
index++;
}
score = score + 10;
canUpdate = true;
}
#Override
protected void onPause() {
super.onPause();
v.pause();
}
#Override
protected void onResume() {
super.onResume();
v.resume();
}
public class MyView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false;
public MyView(Context context) {
super(context);
holder = getHolder();
}
#Override
public void run() {
while (isItOk == true) {
if (!holder.getSurface().isValid()) {
continue;
}
Canvas c = holder.lockCanvas();
if(canUpdate){
canvas_draw(c);
}
holder.unlockCanvasAndPost(c);
try {
t.sleep (50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected void canvas_draw(Canvas canvas) {
canvas.drawARGB(255, 50, 10, 10);
String ranString = "Score " + score;
ran = Math.random() * 5;
String t = "max1" + test1;
String t2 = "max2" + test2;
if (ran > 3) {
createEnemies();
}
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(15);
canvas.drawText(hpString, 10, 25, paint);
canvas.drawText(ranString, 10, screenHeight - 25, paint);
for (Sprite sprite : copy) {
sprite.sprite_draw(canvas);
}
Player.sprite_draw(canvas, copy);
}
public void pause() {
isItOk = false;
while (true) {
try {
t.join();
} catch (InterruptedException e) {
}
break;
}
t = null;
}
public void resume() {
isItOk = true;
t = new Thread(this);
t.start();
}
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
return false;
}
public static void damagePlayer() {
hpString = "Health " + playerHealth;
playerHealth = playerHealth - 5;
if(playerHealth<0){
hpString = "game over";
}
}
public static void setDirection(int i, int j) {
if(i == 0 && j == -1){
for(Sprite s: copy){
s.y++;
}
}
if(i == 0 && j == 1){
for(Sprite s: copy){
s.y--;
}
}
if(i == -1 && j == 0){
for(Sprite s: copy){
s.x++;
}
}
if(i == 1 && j == 0){
for(Sprite s: copy){
s.x--;
}
}
}
}
So the first part of my question is can anyone tell me why the for loops wont stop looping. And the second part of my question is how can I force the checkX and checkY methods to restart if the player selects a different point before he makes it to the first location.
Check and see if your problem lies with your signature in your checkX and checkY functions. Integer is a class, and int is a primitive type, so you can't do something like Integer n = 1, but you can do int n = 1.
public void checkX(int touchx)
public void checkY(int touchx)
Can anyone help, my app does not seem to do the onDraw() in the GraView class, even if .invalidate is called upon from either inside or outside the class. I managed to figure out with system.err that it does invalidate the view, however it never gets in the onDraw( ), even though the rest of the app keeps running. I found a lot of solutionssuggesting putting setWillNotDraw(false) in the constructor, but that did not solve anything.
GravIO.java File:
package dabawi.gravitas;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class GravIO extends Activity implements Runnable, OnTouchListener {
public final static int clock = 1000;
private GravEngine engine;
private GraView TV;
private SensorManager mSensorManager;
private Sensor mSensor;
private Thread t1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
t1 = new Thread(this);
t1.start();
engine = new GravEngine();
TV = new GraView(this, engine);
setContentView(TV);
TV.setOnTouchListener(this);
TV.setVisibility(View.VISIBLE);
//mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
System.err.println("Starting Engine") ;
run();
}
#Override
public void run() {
while (true) {
try {
System.err.println("Tik") ;
engine.tick();
TV.invalidate();
Thread.sleep(GravIO.clock);
} catch (InterruptedException ex) {
System.err.println("faal");
}
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
engine.switchGravity();
return true;
}
}
GraView.java File:
package dabawi.gravitas;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
#SuppressLint({ "DrawAllocation", "ViewConstructor" })
public class GraView extends View {
private GravEngine engine;
private int screenWidth, screenHeight, ySpace = 5, xSpace, scale;
private Paint paint;
private int xLoc, yLoc;
private boolean xWall = false, yWall = false;
public GraView(Context context, GravEngine engine) {
super(context);
setWillNotDraw(false);
this.engine = engine;
calcScale(context);
}
#Override // We think the problem is with this method, that it is never called upon.
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.err.println("Calculating position");
calcPos();
canvas = new Canvas();
// drawing background
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
drawGame(canvas);
}
private void drawGame(Canvas canvas) {
drawRoom(canvas);
drawPlayer(canvas);
}
#SuppressWarnings("deprecation")
private void calcScale(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display d = wm.getDefaultDisplay();
screenWidth = d.getWidth();
screenHeight = d.getHeight();
scale = screenHeight / ySpace;
xSpace = screenWidth / scale;
}
private void calcPos() {
xLoc = (engine.getxPlayer() / GravEngine.roomScaling);
yLoc = (engine.getyPlayer() / GravEngine.roomScaling);
if (xLoc < (xSpace + 1) / 2) {
xLoc = (xSpace + 1) / 2;
xWall = true;
} else if (xLoc > (GravRoom.xSize - ((xSpace + 1) / 2))) {
xLoc = GravRoom.xSize - ((xSpace + 1) / 2);
xWall = true;
} else {
xWall = false;
}
if (yLoc < (ySpace + 1) / 2) {
yLoc = (ySpace + 1) / 2;
yWall = true;
} else if (yLoc > (GravRoom.xSize - ((ySpace + 1) / 2))) {
xLoc = GravRoom.ySize - ((ySpace + 1) / 2);
yWall = true;
} else {
yWall = false;
}
}
private void drawPlayer(Canvas canvas) {
float xPos = engine.getxPlayer() / GravEngine.roomScaling, yPos = engine
.getyPlayer() / GravEngine.roomScaling;
if (xWall) {
}
if (yWall) {
}
paint.setColor(Color.BLUE);
int left = (int) (xPos * scale), top = (int) (yPos * scale);
int right = left + (GravEngine.pxSize / GravEngine.roomScaling) * scale;
int bot = top + (GravEngine.pySize / GravEngine.roomScaling) * scale;
canvas.drawRect(left, top, right, bot, paint);
}
private void drawRoom(Canvas canvas) {
for (int i = 0, x = xLoc - ((xSpace + 1) / 2); i < xSpace + 1; x++, i++) {
for (int j = 0, y = yLoc - ((ySpace + 1) / 2); i < ySpace + 1; y++, j++) {
drawRoomPart(x,y,i,j,canvas);
}
}
}
private void drawRoomPart(int x, int y, int i, int j, Canvas canvas) {
if (x >= 0 && y >= 0 && x < GravRoom.xSize && y < GravRoom.ySize) {
short type = engine.getRoom(engine.getCurrentRoom()).getGridPos(x,y);
if (type != 0) {
drawBlock(canvas, i, x, j, y, type);
}
}
}
private void drawBlock(Canvas canvas, int i, int x, int j, int y, short type) {
int left = i * scale, top = y * scale;
int right = left + scale;
int bot = top + scale;
paint.setColor(colorBlock(type));
canvas.drawRect(left, top, right, bot, paint);
System.err.println("Left" + left + " top: " + top + " right: "
+ right + " bot: " + bot);
}
private int colorBlock(short type) {
if (type == 1) {
return Color.DKGRAY;
} else if (type == 2) {
return Color.CYAN;
} else if (type == 3) {
return Color.GREEN;
} else if (type == 4) {
return Color.RED;
} else {
return Color.MAGENTA;
}
}
// private void imageBlock(short type) {
//
// }
}
Ouch !
Your call to your run method is executed in the UIThread (as the onCreate method), and it's doing an infinite loop in it.
Just add a println after the call and you will see that the UI thread is never released, your application should not even start !
You don't see any changes because you are creating a new canvas and drawing on that. To fix this, remove the line
canvas = new Canvas();
Also, you should initiate your Paint outside of the onDraw method (this is an optimization recommended by the Android documentation).