# Rect() function not working when used inside of a function inside of a class

**URL:** <https://discourse.processing.org/t/rect-function-not-working-when-used-inside-of-a-function-inside-of-a-class/34347>\
**Category:** Coding Questions\
**Created:** [December 29, 2021, 4:44am UTC](https://discourse.processing.org/t/rect-function-not-working-when-used-inside-of-a-function-inside-of-a-class/34347 "2021-12-29T04:44:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![A\_game\_programmer](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@A\_game\_programmer](https://discourse.processing.org/u/A_game_programmer)\
**Post date:** [December 29, 2021, 4:44am UTC](https://discourse.processing.org/t/rect-function-not-working-when-used-inside-of-a-function-inside-of-a-class/34347/1 "2021-12-29T04:44:03Z")

</div>

Can someone please clarify why the rect() function is not being called?  
I have checked it and code in the if function is being executed as normal, just functions to do with drawing are not working.

> public class FadeCubes{  
> IntList brightval = new IntList();  
> IntList x = new IntList();  
> IntList y = new IntList();  
> FadeCubes(){}  
> void update(){  
> for(int i = 0; i \< brightval.size(); i++){  
> brightval.sub(i,17);  
> if(brightval.get(i) == 0){  
> brightval.remove(i);  
> x.remove(i);  
> y.remove(i);  
> }else{  
> fill(brightval.get(i));  
> rect(x.get(i),y.get(i),20,20);  
> }  
> }  
> println(x);  
> }  
> void addcube(int inx, int iny){  
> brightval.append(255);  
> x.append(inx);  
> y.append(iny);  
> }  
> }

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 29, 2021, 8:04am UTC](https://discourse.processing.org/t/rect-function-not-working-when-used-inside-of-a-function-inside-of-a-class/34347/2 "2021-12-29T08:04:15Z")

</div>

> [@A\_game\_programmer](#):
>
> , just functions to do with drawing are not working.

I’ve added more code so I could test it here and the cubes got drawn for me.

However when you have a loop which got **remove()** inside it you should make it backwards instead!

This is a slightly modified version of your code which includes the backwards loop:

```auto
// https://Discourse.Processing.org/t/rect-function-not-working-
// when-used-inside-of-a-function-inside-of-a-class/34347/2

// 2021-Dec-29

static final int INIT_CUBES = 10;
final FadeCubes cubes = new FadeCubes();

void setup() {
  size(400, 300);
  frameRate(1);
  for (int i = 0; i < INIT_CUBES; ++i) mousePressed();
}

void draw() {
  background(0);
  cubes.update();
  println(frameCount, cubes);
}

void mousePressed() {
  final int
    x = (int) random(width - FadeCubes.SIZE), 
    y = (int) random(height - FadeCubes.SIZE);

  cubes.addCube(x, y);
}

class FadeCubes {
  static final int INIT_BRIGHT = 255, BRIGHT_LOSS = 17, SIZE = 20;

  final IntList brights = new IntList();
  final ArrayList<PVector> positions = new ArrayList<PVector>();

  void addCube(final int x, final int y) {
    brights.append(INIT_BRIGHT);
    positions.add(new PVector(x, y));
  }

  void update() {
    for (int i = brights.size(); --i >= 0; ) {
      brights.sub(i, BRIGHT_LOSS);
      final int gray = brights.get(i);

      if (gray > 0) {
        final PVector pos = positions.get(i);
        fill(gray);
        square(pos.x, pos.y, SIZE);
      } else {
        brights.remove(i);
        positions.remove(i);
      }
    }
  }

  String toString() {
    return "Cubes: " + brights.size();
  }
}

```
