i use arrayList remove, object blink

edited December 2015 in Questions about Code

Hello, I use a arrayList to manage the appearance and disappearance of objects. when the opacity is 0 I delete them with remove. some object are blink. it does not appear if I do not delete objects to 0 opacity. here is my code. thanks for the advice

Shape mShape;
ArrayList<Shape> aShape = new ArrayList<Shape>();
int rouge = 255;
int vert = 255;
int bleu = 255;
int shapeI = 0;
boolean trace = false;
boolean cloture = false;
int frameR = 1;

void settings() {
  fullScreen();
}

void setup() {
  smooth(); //Lissage des dessins
  size(800, 600); //Taille de la fenêtre
  background(0); //Fond noir
  noCursor();
}

void draw() {
  background(0); 
  float tX = random(1, 800);
  float tY = random(1, 600);  
  float tO = 0;
  float o = random(0.1, 4);
  float tW = random(1, 2);
  float oM = random(100, 200);

  if (!cloture) {
    if (shapeI > 0 && shapeI % 100 == 0) {
      aShape.add(new Shape(tX, tY, tO, tW, 0, oM));
    } else {
      aShape.add(new Shape(tX, tY, tO, tW, o, oM));
    }
  }

  for (int i=0; i < aShape.size(); i++) {
    Shape mShape = aShape.get(i);
    if (mShape.getOpacity() < 0) {
      println("-------------");
      println("size::"+aShape.size());
      aShape.remove(i);
      println("-------------");
      println("id::"+i+" size::"+aShape.size());
    } else {
      mShape.update();
      mShape.display();
    }
  }

  if (!cloture) {
    shapeI++;
  }

  //dessine le pinceau avec comme position celle de la souris
  if (trace) {
    if (pmouseX != 0 && pmouseY !=0) {
      line(pmouseX, pmouseY, mouseX, mouseY);
    }
  }
}

class Shape {
  float x;
  float y;
  float w;
  float opacity;
  float opacityMax;
  float o = 0;
  float weight;
  color c;
  PFont police;
  boolean desc = false;

  Shape(float nX, float nY, float nOpacity, float nW, float tO, float oM) {
    x          = nX;
    y          = nY;
    opacity    = nOpacity;
    opacityMax = oM;
    o          = tO;
    w          = nW;
  }

  public double getOpacity () {
    return opacity;
  }

  void update () {
    if (o!=0) {
      if (opacity <= opacityMax && desc==false) {
        opacity += o;
      } else {
        opacity -= o;
        desc = true;
      }
    }
  }

  void display() {
    if (o!=0) {
      stroke(opacity);
      strokeWeight(w);
      ellipse(x, y, w, w);
    } else {
      police = loadFont("Courier-8.vlw");
      textFont(police, 8);
      fill(255,255,255,255);
      text("je", x, y);
    }
  }
}

void keyReleased() {
  if (key == 's'  || key == 'S') {
    trace = true;
  }
  if (key == 'p'  || key == 'P') {
    frameR+=1;
  }
  if (key == 'm'  || key == 'M') {
    frameR-=1;
  }

  if (key == 'f'  || key == 'F') {
    cloture=true;
  }
}

Answers

  • Answer ✓

    If you remove objects from a list, then you should always iterate over that list in reverse order.

      for (int i=aShape.size()-1; i >= 0; i--) {
    
       // removing objects here
    
      }
    

    Otherwise it is likely that you skip objects or run into a NullPointerException.

  • thank you !! it works

Sign In or Register to comment.