# How do I make an array method?

**URL:** https://discourse.processing.org/t/how-do-i-make-an-array-method/42286
**Category:** Coding Questions
**Created:** [June 24, 2023, 5:57pm UTC](https://discourse.processing.org/t/how-do-i-make-an-array-method/42286 "2023-06-24T17:57:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)
#### Post date: [June 24, 2023, 5:57pm UTC](https://discourse.processing.org/t/how-do-i-make-an-array-method/42286/1 "2023-06-24T17:57:08Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2023, 7:31pm UTC](https://discourse.processing.org/t/how-do-i-make-an-array-method/42286/2 "2023-06-24T19:31:38Z")

</div>

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

see [PImage::pixels[] / Reference / Processing.org](https://processing.org/reference/PImage_pixels.html)

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

Instead of

> [@Random\_Ghost](#):
>
> ```auto
> class CImage{
> C3[][] cImageArray;
> int width;
> 
> ```

try

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

```

**Later**

and later

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

```

**Remark**

this

> [@Random\_Ghost](#):
>
> ` cImageArray[x][y] = new C3(img.pixels[index(x, y, width)]);`

can be

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

The formula `x + y *width` can be found here: [set() / Reference / Processing.org](https://processing.org/reference/PImage_set_.html)

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

Warm regards,

Chrisir

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2023, 7:47pm UTC](https://discourse.processing.org/t/how-do-i-make-an-array-method/42286/3 "2023-06-24T19:47:07Z")

</div>

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

cf. [Arrays / Processing.org](https://processing.org/tutorials/arrays)

cf. [Two-Dimensional Arrays / Processing.org](https://processing.org/tutorials/2darray)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [June 24, 2023, 7:55pm UTC](https://discourse.processing.org/t/how-do-i-make-an-array-method/42286/4 "2023-06-24T19:55:00Z")

</div>

> [@Random\_Ghost](#):
>
> ```auto
> C3 getC3[x][y]{
> return imageArray[x][y];
> }  
> 
> ```

try

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

```

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

---

<div class="post-metadata">

### Author: ![Random\_Ghost](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/random_ghost/32/16609_2.png) [@Random\_Ghost](https://discourse.processing.org/u/Random_Ghost)
#### Post date: [June 24, 2023, 8:25pm UTC](https://discourse.processing.org/t/how-do-i-make-an-array-method/42286/5 "2023-06-24T20:25:27Z")

</div>

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.
