Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Can anybody tell me why I'm getting a null pointer on my PImage array??

$
0
0
import processing.serial.*;

import cc.arduino.*;

import static javax.swing.JOptionPane.*;
import javax.swing.JPasswordField;




final StringDict accounts = new StringDict(
  new String[] {
  "john", "tony", "amy"
  }
  , new String[] {
    "ilikedogs", "husky", "pepsi"
  }
  );

final JPasswordField pwd = new JPasswordField();

{
  println(accounts);
}
float m;

PImage startGameBackground;
PImage kitchen;
PImage dog;
float dogx=800, dogy=600;
HealthBar hb0 = new HealthBar();
boolean isWashed=false;

PImage[] images = new PImage[3];


PImage achievment2;
int val;
int stage =1;
Serial port;

//Fountain f = new Fountain();
//Ball b = new Ball(x,y,d,xs,ys);
Ball b = new Ball();


ArrayList<Ball>ballList;
boolean buttonOn = false;

final int HowLongIsItWaiting1 = 900;
int frameNum = 0;


void setup() {


  size(1800, 900);
  String comPort = "COM3";

  stage =1;
  port = new Serial(this, comPort, 9600);

  //f.fountainSetup();
  ballList = new ArrayList<Ball>();
  m = millis();

  startGameBackground = loadImage("startGameBackground.png");
  kitchen = loadImage("kitchen.png");
  dog = loadImage("washDog.png");
  startGameBackground.resize(1800, 900);
  kitchen.resize(1800, 900);
  achievment2 = loadImage("achievment2.png");
  val = port.read();

  images[0] = loadImage("washDog.png");
  images[1] = loadImage("washDog2.png");
  images[2] = loadImage("washDog3.png");

  for ( int i = 0; i < images.length; i++ )
  {
    images[i] = loadImage(i + ".png" );
  }

  frameRate(30);
}




void draw() {



  if (stage ==1) {

    background(startGameBackground);

    if (frameCount == 1)  surface.setVisible(true);


    String user = askUser();

    if (user == null)           confirmQuit();
    else if (!"".equals(user))  askPass(user);
  } else if (stage ==2) {





    background(kitchen);
    image(dog, dogx, dogy);
    hb0.draw();

    isWashed =true;

    if (mousePressed ==true) {
      float xSpeed = (float)(-3 + Math.random()*6);
      Ball b = new Ball(880, 400, 20, xSpeed, -2);

      b.dropColour = color(204, 255, 255);

      ballList.add(b);
    }


    for (int i =0; i <ballList.size(); i++) {
      Ball b2 = ballList.get(i);



      fill(b2.dropColour);
      ellipse(b2.x, b2.y, b2.diameter, b2.diameter);
      b2.move();
      //println("working");


      b2.isDead();

      if (b2.isAlive == false)
        ballList.remove(b2);








      if (hb0.value <100) {


        port.write(2);
        //delay(1000);
        //for(Ball b:ballList){
        b2.isDead();

        if (b2.isAlive == false) {
          ballList.remove(b2);
          hb0.value += .5;
          println("A ball was destroyed");
        } else {
          println("A ball survived");
        }
      }
      //} else if (hb0.value >= hb0.max) {
        frameNum++;
        frameNum %= images.length;
        image(images[frameNum], 800, 600);

        port.write(1);


        println("Here is the achievment");
        image(achievment2, 700, 100);



        if (millis()-m>HowLongIsItWaiting1) {



          exit();
        }
      }
    }
  }
}











String askUser() {
  String id = showInputDialog("Please enter user:");

  if (id == null)
    showMessageDialog(null, "You've canceled login operation!"
      , "Alert", ERROR_MESSAGE);

  else if ("".equals(id))
    showMessageDialog(null, "Empty user input!"
      , "Alert", ERROR_MESSAGE);

  else if (!accounts.hasKey(id = id.toLowerCase()))
    showMessageDialog(null, "Unknown \"" + id + "\" user!" + (id = "")
      , "Alert", ERROR_MESSAGE);

  return id;
}

boolean askPass(String id) {
  boolean isLogged = false;
  pwd.setText("");

  int action = showConfirmDialog(null, pwd
    , "Now enter password:", OK_CANCEL_OPTION);

  if (action != OK_OPTION) {
    showMessageDialog(null, "Password input canceled!"
      , "Alert", ERROR_MESSAGE);

    return false;
  }

  String phrase = pwd.getText();
  //String phrase = new String(pwd.getPassword());

  if ("".equals(phrase))
    showMessageDialog(null, "Empty password input!"
      , "Alert", ERROR_MESSAGE);

  else if (accounts.get(id).equals(phrase)) {
    showMessageDialog(null, "Welcome \"" + id + "\"!\nYou're logged in!"
      , "Info", INFORMATION_MESSAGE);

    isLogged = true;
    stage=2;
  } else
    showMessageDialog(null, "Password \"" + phrase + "\" mismatch!"
      , "Alert", ERROR_MESSAGE);

  return isLogged;
}

void confirmQuit() {
  if (showConfirmDialog(null, "Wanna quit then?", "Exit"
    , OK_CANCEL_OPTION) == OK_OPTION)  exit();
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles