PImage in class

#1

When I try to create a PImage instance variable in a class file, I get the following error:

Cannot find a class or type named “PImage”

Does anyone know why this is? For reference, the (simplified) class looks like this:

class MyObj {
  PImage image;
  
  MyObj(String imgName) {
    image = loadImage(imgName);
  }
}

The error occurs on line 2. I really appreciate the help. I just don’t get what the problem is here, since using PImage in my main file works fine.

1 Like
#2

Is it a “.pde” or a “.java” file? :grey_question:

#3

-a- i not recommend your naming a variable “image”
as it is a function to draw a image ( as texture on a quad vertex)

-b-
but basically the idea works here
win 7 / 64bit / processing 3.5.3 JAVA mode

String infile = "data/moonwalk.jpg";

class MyImg {
  PImage img;
  MyImg(String imgName) {
     img = loadImage(imgName); 
  }
}

MyImg thepicture;

void setup() {
  size(640, 360);
  thepicture = new MyImg(infile);  
}

void draw() {
  image(thepicture.img,0,0);
}

#4

It is a “.java” file.

#5

a. Understood. I can change the variable name, but I’m pretty sure that isn’t creating the problem here.

b. Are you just suggesting that I put the MyObj (or MyImg) class in the same file as my setup and draw methods? I could do this, but it seems fairly inconvenient. Will it never work if it’s a seperate “.java” file?

Also for the record, I am on OSX Mojave, with Processing 3.4. I also quickly tested 3.5.3 and got the same error.

#6

So you need to import it: import processing.core.PImage;
Processing.GitHub.io/processing-javadocs/core/processing/core/PImage.html

Also, prefer to ask for an already loaded PImage rather than making your class loadImage() by itself:

MyObj.java:

import processing.core.PImage;

public class MyObj {
  PImage img;

  public MyObj(final PImage pic) {
    img = pic;
  }
}
1 Like
#7

Great. Thank you so much!

#8

the usual Processing way would be open a other TAB “classtab” in the IDE ( what would create a other file “classtab.pde”