How do I make an array method?

I am currently trying to make a CImage class similar to the PImage class. But I want to have a method for accessing pixel values from the image array in the form img.pixels[y].
How do I set up the method that way?
Here’s the some of the code

class CImage{
  C3[][] cImageArray;
  int width;
  int height;
  
  CImage (PImage img){
    width = img.width;
    height = img.height;
    cImageArray = new C3[width][height];
    for (int y = 0; y < height; y++) {
      for (int x = 0; x < width; x++) {
        cImageArray[x][y] = new C3(img.pixels[index(x, y, width)]);
      }
    }
  }
  /*
  C3 getC3[x][y]{
    return imageArray[x][y];
  }  
  */
  PImage toPImage(){
    PImage img = new PImage(width, height, RGB);
    for(int y = 0; y < height; y++){
      for(int x = 0; x < width; x++){
        img.pixels[index(x, y, width)] = cImageArray[x][y].toColor();
      }
    }
    return img;
  }  
}  

It’s the getC3 method. Or do I just use object.cImageArray[y]?
I am also trying to make a set C3 method in that fashion.

when you check out the reference you’ll see, pixels is an 1D (list) of color

see PImage::pixels[] / Reference / Processing.org

instead of a list you plan to use a grid. Which is ok.

Instead of

try

class CImage{
  color[][] cImageArray;
  int width;

Later

and later

CImage (PImage img){
    width = img.width;
    height = img.height;
    cImageArray = new color[width][height];

Remark

this

can be

cImageArray[x][y] = img.pixels[ x + y *width ] ;

The formula x + y *width can be found here: set() / Reference / Processing.org

You try C3 which is unnecessary you can stick with color.

Warm regards,

Chrisir

an array is not a method it’s a data structure

cf. Arrays / Processing.org

cf. Two-Dimensional Arrays / Processing.org

try

  color getColorFromImageArray (int x, int y) {
    return imageArray[x][y];
  }  

(Same would apply when you use C3 instead of color btw)

Yes, that is the method I used. I created an index method to calculate the index in the pixel array. And I am using a C3 class as a replacement for the color class also.
I think I’d just use object.cImageArray[x][y] instead as this is similar to the object.pixels[x] in the PImage class.
Thanks for your help though.

1 Like