Pregunta

I am working on a project for university. I have only recently started learning to code and would really appreciate any help I can get. I am using processing and have imported the following libraries: xlsReader & ControlP5. I have created an object that is drawn 7 times.

My first attempt to color the objects using 7 specific colors for the 7 repetitive objects can be seen in the code below. I am still trying to figure this out but at the moment my objects are only reading the last color of the array. If anyone can please give me some advice or offer a different solution than using an array, I would be so grateful.

A piece of my coding is as follow:

if (showData == true) {
  for (int row = 0; row < 6; row++) {

    xPinBusiness = reader.getInt(row+1, col);

    int xPin1 = int(map(xPinBusiness, 0, 3000, amountsStart, amountsEnd));          

    color[] myColors = new color[7];
      myColors[0] = color(208, 102, 155);
      myColors[1] = color(64, 161, 177);
      myColors[2] = color(99, 153, 98);
      myColors[3] = color(101, 102, 156);  
      myColors[4] = color(255, 204, 87);
      myColors[5] = color(255, 154, 22);
      myColors[6] = color(255, 53, 41);    


for (int colorRange = 0; colorRange < myColors.length; colorRange+=1){

    pin1.display(myColors[colorRange], xPin1, 295, 20);


    }
  }
}
¿Fue útil?

Solución

The problem is that the draw() function goes through your for loop and then uses the end result for the value of color, which is the last value in your array, as the color that it passes into your function. In other words, your GUI is basically frozen or locked while the for loop runs so the intermediate values, even though they are considered inside the loop, don't have any affect on whatever is being drawn on the screen.

Simplify your draw() function by removing the loop and doing something like the example code below (I used your array of colors for this example):

int colorCounter;

void setup()
{
  // slowed down the frameRate so you can see the effect of colors changing.
  frameRate(5);
  colorCounter = 0;
}

void draw()
{
  color[] myColors = new color[7];
  myColors[0] = color(208, 102, 155);
  myColors[1] = color(64, 161, 177);
  myColors[2] = color(99, 153, 98);
  myColors[3] = color(101, 102, 156);  
  myColors[4] = color(255, 204, 87);
  myColors[5] = color(255, 154, 22);
  myColors[6] = color(255, 53, 41); 

  fill(myColors[colorCounter]);

  ellipse(50, 50, 50, 50);

  println(colorCounter);

  // using % to wrap around and avoid NPE
  colorCounter = (colorCounter + 1) % myColors.length;
}

It will help to remember that draw() itself is an infinite loop so no need to add other loops inside of it. If you think you need a loop inside draw() then more often than not, you need to rethink your strategy because in most cases, you can just create a counter, like colorCounter in this example, and use it to control whatever it is you want to control within the draw() function.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top