using images as objects and importing them

so im working on a program and need to import images into the class but also use objects, so i am trying to display the object which is the image but i keep getting a nullpointerexception... i havent used objects in this way before so i dont really have an idea of what im doing or where the npe error could be.. if anyone could help that would be great because being stuck on code this early really sucks...

here is my main class

//lab 4 

Boat boat; //calls the boat object


void setup(){
  size(600,400);
  background(255);
}

void draw(){
  boat.setLocation(mouseX,mouseY);
  boat.display();
}

and here is the class for the object

class Boat {
  PImage boat;
  float x,y;
  void ship() {
  }
  void setLocation(float tempX, float tempY) {
    x= tempX;
    y= tempY;
  }

    void display() {
      image(boat, 5, 50);
    }
  }

also it is showing the error on line 12 of the main class

addon have another question and was told to post it here

so im back again, i need to use an array to display an image and cant really seem to get it currently i have a if mousePressed statement because im not sure how to initialize the if statement to loop the array i guess?

here is what i have

main class

PImage img_boat;
PImage img_Iceberg;
Boat my_boat;
Iceberg[] my_iceberg;
float mouseX=50;
float mouseY;
float posX=5;
float posY=150;
int totalIceberg=0;

void setup() {
  size(800, 400);
  background(255);
  img_boat=loadImage("imgBoat.png");
  my_boat=new Boat();
  my_iceberg = new Iceberg[75];
}
void draw() {
  background(31, 80, 222);
  my_boat.setLocation(mouseX, mouseY);
  my_boat.display();
}

void mouseClicked() {
  if (mousePressed==true) {
    my_iceberg[totalIceberg] = new Iceberg();
    totalIceberg++;
    if ( totalIceberg >= my_iceberg.length)
      totalIceberg = 0;
  }
}


void keyPressed() {
  if (keyPressed) {
    if (key =='W' || key == 'w') {
      posY--;
    }
    if (key== 'd' || key=='D') {
      posX++;
    }
    if (key== 'a' || key =='A') {
      posX--;
    }
    if (key== 's' || key =='S') {
      posY++;
    }
  }
}

class Boat {
  float x, y;


  Boat() {
    x=width/2;
    y=height/2;
  }
  void setLocation(float tempX, float tempY) {
    x=tempX;
    y=tempY;
  }
  void display() {
    image(img_boat, posX, posY);
    img_boat.resize(80, 80);
  }
}

class Iceberg {
  float x, y; //variables for location
  float speed; //variabke for speed
  float c;
  float r;

  Iceberg() {
    x=125;
    y=25;
    speed=random(1, 3);
    r=6;
    c=color(30,60,150);
    img_Iceberg=loadImage("iceberg.png");
  }
  void setLocation(float tempoX, float tempoY) {
    x=tempoX;
    y=tempoY;
  }

  void move() {
    x=x+speed;
    if (x> width) x=0;
    y=y-speed;
    if (y<-50)y=400;
  }


  void display() {  //displays iceberg
    fill(c);
    noStroke();
    image(img_Iceberg,x,y);
  }
}

outcome of this is its a game where you need to navigate your ship through the moving icebergs

not sure what im missing or what i could solve it with since rn my icebergs arent displaying, i had a .display function being used but the example im using doesnt use that also it doesnt use an image soo.

thanks in advance seem everyone here always seems to help

Answers

  • edited April 2018

    The way you have written it, right now, is that each Boat has its own image.

    These images could be different! That is, each Boat could load a different image, and that image would be what that Boat looks like.

    This is probably not what you want.

    If all of your Boats are going to look the same, you don't need to give each Boat its own image. You'd only need one image, and every boat can use that one image.

    Because you have posted some properly formatted code that demonstrates your issue, you get a gold star, and I can fix the code for you:

    PImage img_boat; // The one image all Boats use.
    Boat my_first_boat; // Space for a Boat.
    Boat my_second_boat; // Space for a second Boat.
    
    void setup() {
      size(600, 400);
      background(255);
      img_boat = loadImage("boat_filename.PNG"); // Load the image once.
      my_first_boat = new Boat(); // Create this Boat.
      my_second_boat = new Boat(); // Create this Boat too.
    }
    
    void draw() {
      background(100,100,200); // Draw a blue sea.
      my_first_boat.setLocation(mouseX, mouseY); // Move the first Boat.
      my_first_boat.display(); // Put the first Boat out there.
      my_second_boat.display(); // Put the other Boat out there too.
    }
    
    class Boat {
      float x, y;
      Boat() {
        // New Boats always start in the middle of the screen.
        x = width/2;
        y = height/2;
      }
      void setLocation(float tempX, float tempY) {
        x = tempX;
        y = tempY;
      }
      void display() {
        // All the Boats look the same!
        image(img_boat, 5, 50);
      }
    }
    
  • edited April 2018

    TF's lines 9 and 10 are important and missing in your original code. It's not enough just to define the boat, you also have to instantiate it. That's what calling new does.

  • alright so i have my code as you have shown i should and no image is appearing

    //lab 4 
    PImage boat;
    Boat my_boat;
    
    
    void setup() {
      size(600, 400);
      background(255);
      boat=loadImage("boat.png");
      my_boat= new Boat(); //create the boat
    }
    
    void draw() {
      background(100, 100, 200); //draws the blue sea
      my_boat.setLocation(mouseX, mouseY);
      my_boat.display();
    }
    
    
    class Boat {
      float x, y;
      Boat() {
        //new boat will start in middle of screen
        x= width/2;
        y=height/2;
      }
    
      void setLocation(float tempX, float tempY) {
        x= tempX;
        y= tempY;
      }
    
      void display() {
        image(boat, 5, 50);
      }
    }
    

    im not sure if im missing something or what i have it how you do

  • Answer ✓

    Few things. The trivial first:

    setLocation() sets x and y. However, display() uses 5,50. Instead do this. image(boat,x,y);

    Second point. I changed your code. Study this new version and reflect (or ask) what advantages you get doing it this way instead.

    Kf

    Boat my_boat;     
    
    void setup() {
      size(600, 400);
      background(255);      
      my_boat= new Boat( loadImage("boat.png") ); //create the boat
    }
    
    void draw() {
      background(100, 100, 200); //draws the blue sea
      my_boat.setLocation(mouseX, mouseY);
      my_boat.display();
    }
    
    
    class Boat {
      float x, y;
      PImage boat;
    
      Boat(PImage apic) {
        //new boat will start in middle of screen
        x= width/2;
        y=height/2;
        boat=apic;
      }
    
      void setLocation(float tempX, float tempY) {
        x= tempX;
        y= tempY;
      }
    
      void display() {
        image(boat, x, y);
      }
    }
    
  • i do this now and still no image displays i dont know why

  • sooo i used a different image and it displays now

  • Great to hear.

    Kf

Sign In or Register to comment.