Quantcast
Channel: Android Mode - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 941

How can I load an image onto the tablet as an animated wallpaper?

$
0
0

I am trying to make an animated wallpaper for my Samsung Galaxy Tab A6 running Android 5.1. When I launch the code and try to set it as my wallpaper, I get the error: "Unfortunately, PongWallpaper has stopped." I was trying to debug and found that it only gives me this error when I try to load an image. The Processing shapes that I have coded run fine. The images that I am trying to load are in a folder called "Sprites" inside the sketch folder.

PImage AndroidApple, Arduino, Atari, Google, Python, RaspPi, Windows;
PImage[] ImgLineup = new PImage[7];
int currentImg = 0;
float lastImgTime;

Ball ball;
Paddle redPaddle, bluePaddle;
float[] paddleSpeeds = {-5, 5};
float[] ballSpeeds = {-5, 5};
int[] YesOrNoChoice = {0,1};

boolean CanHitRed = true, CanHitBlue = true;

color white = color(255, 255, 255);
color black = color(0, 0, 0);
color gold = color(255, 217, 84);
color red = color(255, 109, 84);
color blue = color(84, 132, 255);

void setup() {
  fullScreen();
  rectMode(CENTER);
  imageMode(CENTER);

  // images
  AndroidApple = loadImage("Sprites\\AndroidApple.png");
  Arduino = loadImage("Sprites\\Arduino.png");
  Atari = loadImage("Sprites\\Atari.png");
  Google = loadImage("Sprites\\Google.png");
  Python = loadImage("Sprites\\Python.png");
  RaspPi = loadImage("Sprites\\RaspPi.png");
  Windows = loadImage("Sprites\\Windows.png");

  AndroidApple.resize(width/2, height/2);
  Arduino.resize(width/4, height/2);
  Atari.resize(width/2, height/2);
  Google.resize(width/4, height/2);
  Python.resize(width/4, height/2);
  RaspPi.resize(width/4, height/2);
  Windows.resize(width/4, height/2);

  ImgLineup[0] = AndroidApple;
  ImgLineup[1] = Arduino;
  ImgLineup[2] = Atari;
  ImgLineup[3] = Google;
  ImgLineup[4] = Python;
  ImgLineup[5] = RaspPi;
  ImgLineup[6] = Windows;

  lastImgTime = millis();
  /////////////////////////////////////////////////////////////////

  ball = new Ball (width/2, height/2, 5);
  redPaddle = new Paddle (width/8, height/2, paddleSpeeds[floor(random(0,2))], red);
  bluePaddle = new Paddle (width - (width/8), height/2, paddleSpeeds[floor(random(0,2))], blue);
}

void draw() {
  drawGameBoard();

  if (millis() - lastImgTime >= 5000) {
    changeImg();
    lastImgTime = millis();
  }
  image(ImgLineup[currentImg], width/2, height/2);

  ball.checkPos();
  ball.drawBall();
  ball.animate();

  redPaddle.drawPaddle();
  redPaddle.checkPos();
  redPaddle.checkCollRed(ball);
  redPaddle.chooseDirRed();
  redPaddle.animate();

  bluePaddle.drawPaddle();
  bluePaddle.checkPos();
  bluePaddle.checkCollBlue(ball);
  bluePaddle.chooseDirBlue();
  bluePaddle.animate();
}

void drawGameBoard() {
  background(black);
  for(int i = 0; i <= height; i+=50) {
    fill(white);
    stroke(white);
    rect(width/2 - 5, i, 10, 40);
  }
}

void changeImg() {
  if (currentImg <= 5) {
    currentImg += 1;
  } else {
    currentImg = 0;
  }
}

class Ball {
  float xpos, ypos, speed, xSpeed, ySpeed, lastGoalTime;
  int BallWidth, BallHeight;
  Ball (float xpos, float ypos, float speed) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.speed = speed;
    this.xSpeed = this.speed;
    this.ySpeed = -this.speed;
    this.BallWidth = width/32;
    this.BallHeight = BallWidth;
  }

  void drawBall() {
    fill(gold);
    stroke(gold);
    ellipse(this.xpos, this.ypos, this.BallWidth, this.BallHeight);
  }

  void animate() {
    this.xpos += this.xSpeed;
    this.ypos += this.ySpeed;
  }

  void checkPos() {
    // check for 'CanHit' variables
    if (this.xpos > width/2) {
      CanHitRed = true;
    }
    if (this.xpos < width/2) {
      CanHitBlue = true;
    }

    if (this.xpos <= 0 || this.xpos >= width) {
      // stop the ball's speed
      this.xSpeed = 0;
      this.ySpeed = 0;
      // bring the ball back to the center of the screen
      this.xpos = width/2;
      this.ypos = height/2;
      // find the time
      this.lastGoalTime = millis();
    }

    if (this.ypos <= 0 || this.ypos >= height) {
      this.ySpeed = -this.ySpeed;
    }

    if (this.xSpeed == 0 && this.ySpeed == 0) {
      if (millis() - this.lastGoalTime >= 2000) {
        this.xSpeed = ballSpeeds[floor(random(0,2))];
        this.ySpeed = ballSpeeds[floor(random(0,2))];
      }
    }

  }

}

class Paddle
{
  float xpos, ypos, speed, upSpeed, downSpeed, lastDirTime;
  float PaddleWidth, PaddleHeight;
  color paint;
  Paddle (float xpos, float ypos, float speed, color paint) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.speed = speed;
    this.upSpeed = -speed;
    this.downSpeed = speed;
    this.lastDirTime = millis();
    this.paint = paint;
    this.PaddleWidth = width/32;
    this.PaddleHeight = height/8;
  }

  void drawPaddle() {
    fill(this.paint);
    stroke(this.paint);
    rect(this.xpos, this.ypos, this.PaddleWidth, this.PaddleHeight);
  }

  void animate() {
    this.ypos += this.speed;
  }

  void checkPos() {
    if (this.ypos <= 0 || this.ypos >= height) {
      this.speed = -this.speed;
    }
  }

  void checkCollRed(Ball ball) {
    if (CanHitRed == true) {
      if ( (ball.xpos - ball.BallWidth/2) <= (this.xpos + this.PaddleWidth/2) && (ball.xpos + ball.BallWidth/2) >= (this.xpos - this.PaddleWidth/2) &&
      (ball.ypos + ball.BallHeight/2) >= (this.ypos - this.PaddleHeight/2) && (ball.ypos - ball.BallHeight/2) <= (this.ypos + this.PaddleHeight) ) {
        ball.xSpeed = -ball.xSpeed;
        ball.ySpeed = -ball.ySpeed;
        CanHitRed = false;
      }
    }
  }

  void checkCollBlue(Ball ball) {
    if (CanHitBlue == true) {
      if ( (ball.xpos + ball.BallWidth/2) >= (this.xpos - this.PaddleWidth/2) && (ball.xpos - ball.BallWidth/2) <= (this.xpos + this.PaddleWidth/2) && (ball.ypos + ball.BallHeight/2) >= (this.ypos - this.PaddleHeight/2) && (ball.ypos - ball.BallHeight/2) <= (this.ypos + this.PaddleHeight) ) {
        ball.xSpeed = -ball.xSpeed;
        ball.ySpeed = -ball.ySpeed;
        CanHitBlue = false;
      }
    }
  }

  void chooseDirRed() {
    if ( millis() - this.lastDirTime >= 10000 ) {
      if ( YesOrNoChoice[floor(random(0,2))] == 1 ) {
        this.speed = -this.speed;
        this.lastDirTime = millis();
      }
    }
  }

  void chooseDirBlue() {
    if ( millis() - this.lastDirTime >= 8000 ) {
      if ( YesOrNoChoice[floor(random(0,2))] == 1 ) {
        this.speed = -this.speed;
        this.lastDirTime = millis();
      }
    }
  }

}

Viewing all articles
Browse latest Browse all 941