Need help rotating flags every 2 seconds

I’m trying to write a program that draws 4 flags of different countries and rotates them every 2 seconds.
For example it starts out as:
Iceland Italy
Russia Sweden

After two seconds it should be:
Italy Russia
Sweden Iceland

The code has several levels in it, this one is level 4 (nivel_4()). The modified isort is supposed to change the order of the flags in the array. That part is working fine I think. The problem is running this every 2 seconds. I tried to use a while loop (that one in nivel_4()) but it doesn’t display anything. If I comment it out and copy paste the lines that update and print the flags I can see it’s working.

How do I do this every 2 seconds?

https://pastebin.com/NccWSQtF

here is a timer

``````
int timerLevel=0;

void setup() {
size(1460, 850);
timerLevel=millis();
}

void draw() {
if (millis()-timerLevel>4000) {
println("here");
timerLevel=millis();
}
}
``````

Doesn’t work when I try to apply it in the function nivel_4();

1 Like

hey and welcome to the forum!

Great to have you here !

1 Like

I don’t fully understand the code.

There are errors in it, you can’t test Strings with ==, you need to use .equals()…

Here is a version that does something every 2 seconds

Chrisir

``````
final int windowWidth = 480;
final int windowHeight = windowWidth;
int timer;
float width_flag = windowWidth/3;

int n=4;
Flag f[] = new Flag[n];

Coords cr[] = new Coords[n];

void settings() {

size(windowWidth, windowHeight);

get_coords_nivel_2(cr);

for (int i = 0; i < n; i++) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, 0, 0);
}

f[0] = make_Iceland_flag(f[0]);
f[1] = make_Italian_flag(f[1]);
f[2] = make_Russian_flag(f[2]);
f[3] = make_Swedish_flag(f[3]);
}

//////////////////////////////////////
/////////////////////////////////////

class Rectangle {

float x;
float y;
float width_rect;
float height_rect;
color c;

Rectangle(float x, float y, float width_rect, float height_rect, color c) {
this.x = x;
this.y = y;
this.width_rect = width_rect;
this.height_rect = height_rect;
this.c = c;
}

void draw_rect() {
rectMode(CENTER);  // Set rectMode to CENTER
fill(this.c);
noStroke();
rect(this.x, this.y, this.width_rect, this.height_rect);
}
}

class Flag {
String name;
float x; // horizontal coordinate of upper left corner
float y; // vertical coordinate of upper left corner
float width_flag; // lenght of side
float height_flag;
color c;
Rectangle r[] = new Rectangle[1000];
int nr_rects;

Flag (float x, float y, float width_flag, color c, int nr_rects) {
//this.name = name;
this.x = x;
this.y = y;
this.width_flag = width_flag;
this.height_flag = width_flag*(2.0/3.0);
this.c = c;
this.nr_rects = nr_rects;
}

void draw_Flag() {

rectMode(CENTER);  // Set rectMode to CENTER
fill(this.c);  // Set fill to gray
noStroke();
rect(this.x, this.y, this.width_flag, this.height_flag);  // Draw gray rect using CENTER mode
for (int i = 0; i < this.nr_rects; i++) {
r[i].draw_rect();
}

//draw(this.r[i]);
}
}
////////////////////////////////////////
///////////////////////////////////////

////////////////////////////////////////
//                                   //
//          Nivel 1                 //
//                                 //
////////////////////////////////////

Flag make_Iceland_flag(Flag f) {

//Bandeira da Islandia
f.name = "Islandia";
f.c = color(0, 0, 255);

//barra branca vertical
float b_b_v_width = f.width_flag/7;
float b_b_v_height = f.height_flag;
float b_b_v_x = (f.x+f.width_flag/2) - (f.width_flag/3)*2;
float b_b_v_y = (f.y);
color white = color(255, 255, 255);

f.r[0] = new Rectangle(b_b_v_x, b_b_v_y, b_b_v_width, b_b_v_height, white);

//

//barra branca horizontal
float b_b_h_width = f.width_flag;
float b_b_h_height = f.width_flag/7;
float b_b_h_x = (f.x);
float b_b_h_y = (f.y);
//color red = color(255,0,0);

f.r[1] = new Rectangle(b_b_h_x, b_b_h_y, b_b_h_width, b_b_h_height, white);
//

//

////barra vermelha vertical
float b_v_v_width = f.width_flag/15;
float b_v_v_height = f.height_flag;
float b_v_v_x = (f.x+f.width_flag/2) - (f.width_flag/3)*2;
float b_v_v_y = (f.y);
color red = color(255, 0, 0);

f.r[2] = new Rectangle(b_v_v_x, b_v_v_y, b_v_v_width, b_v_v_height, red);//mudar ordem
//

///barra vermelha horizontal
float b_v_h_width = f.width_flag;
float b_v_h_height = f.width_flag/15;
float b_v_h_x = (f.x);
float b_v_h_y = (f.y);
//color red = color(255,0,0);

f.r[3] = new Rectangle(b_v_h_x, b_v_h_y, b_v_h_width, b_v_h_height, red);//mudar ordem
//

f.nr_rects = 4;

return f;
}

void nivel_1() {

float width_flag = windowWidth/3;
float center_x = windowWidth/2;
float center_y = windowHeight/2;
color blue = color(0, 0, 255);

//Bandeira da Islandia

Flag f = new Flag(center_x, center_y, width_flag, blue, 0);

f = make_Iceland_flag(f);

f.draw_Flag();
}
///////////////////////////////////
//////////////////////////////////

////////////////////////////////
//                           //
//         Nivel 2          //
//                         //
////////////////////////////

class Coords {

float x;
float y;

Coords(float x, float y) {
this.x = x;
this.y = y;
}
}

void get_coords_nivel_2(Coords cr[]) {

cr[0] = new Coords(windowWidth/4, windowHeight/4);

cr[1] = new Coords(windowWidth/4*3, windowHeight/4);

cr[2] = new Coords(windowWidth/4, windowHeight/4*3);

cr[3] = new Coords(windowWidth/4*3, windowHeight/4*3);
}

void nivel_2() {

float width_flag = windowWidth/3;
//float x = windowWidth/4;
//float y = windowHeight/4;
//float coords[][] = {{windowWidth/4,windowHeight/4}};
color blue = color(0, 0, 255);

Coords cr[] = new Coords[4];
get_coords_nivel_2(cr);

Flag f[] = new Flag[4];
//Flag f;
for (int i = 0; i < 4; i++) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, blue, 4);
f[i] = make_Iceland_flag(f[i]);
f[i].draw_Flag();
}
}

///////////////////////////////////
//////////////////////////////////

////////////////////////////////
//                           //
//        Nivel 3           //
//                         //
////////////////////////////

Flag make_Swedish_flag(Flag f) {

//Bandeira da Suecia

f.name = "Suecia";
f.c = color(0, 0, 255);

//barra branca vertical
float b_b_v_width = f.width_flag/7;
float b_b_v_height = f.height_flag;
float b_b_v_x = (f.x+f.width_flag/2) - (f.width_flag/3)*2;
float b_b_v_y = (f.y);
color yellow = color(255, 255, 0);

f.r[0] = new Rectangle(b_b_v_x, b_b_v_y, b_b_v_width, b_b_v_height, yellow);

//

//barra branca horizontal
float b_b_h_width = f.width_flag;
float b_b_h_height = f.width_flag/7;
float b_b_h_x = (f.x);
float b_b_h_y = (f.y);
//color red = color(255,0,0);

f.r[1] = new Rectangle(b_b_h_x, b_b_h_y, b_b_h_width, b_b_h_height, yellow);
//

f.nr_rects = 2;

return f;
}

Flag make_Russian_flag(Flag f) {

//Bandeira da Russia

f.name = "Russia";
f.c = color(255, 255, 255);

//barra branca horizontal
float b_b_h_width = f.width_flag;
float b_b_h_height = f.height_flag/3;
float b_b_h_x = (f.x);
float b_b_h_y = (f.y);
color blue = color(0, 0, 255);

f.r[0] = new Rectangle(b_b_h_x, b_b_h_y, b_b_h_width, b_b_h_height, blue);
//

//f.c = color(255,0,0);

//barra vermelha horizontal

float b_v_h_width = f.width_flag;
float b_v_h_height = f.height_flag/3;
float b_v_h_x = (f.x);
float b_v_h_y = (f.y) + (f.height_flag/3);
color red = color(255, 0, 0);

f.r[1] = new Rectangle(b_v_h_x, b_v_h_y, b_v_h_width, b_v_h_height, red);
//

f.nr_rects = 2;

return f;
}

Flag make_Italian_flag(Flag f) {

//Bandeira da Italia

f.name = "Italia";
f.c = color(0, 255, 0);

//barra branca vertical
float b_b_v_width = f.width_flag/3;
float b_b_v_height = f.height_flag;
float b_b_v_x = (f.x);
float b_b_v_y = (f.y);
color white = color(255, 255, 255);

f.r[0] = new Rectangle(b_b_v_x, b_b_v_y, b_b_v_width, b_b_v_height, white);

//

////barra vermelha vertical
float b_v_v_width = f.width_flag/3;
float b_v_v_height = f.height_flag;
float b_v_v_x = (f.x) + (f.width_flag/3);
float b_v_v_y = (f.y);
color red = color(255, 0, 0);

f.r[1] = new Rectangle(b_v_v_x, b_v_v_y, b_v_v_width, b_v_v_height, red);//mudar ordem
//

f.nr_rects = 2;

return f;
}

void nivel_3() {

float width_flag = windowWidth/3;

get_coords_nivel_2(cr);

Flag f[] = new Flag[4];

for (int i = 0; i < 4; i++) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, 0, 0);
}

f[0] = make_Iceland_flag(f[0]);
f[0].draw_Flag();

f[1] = make_Italian_flag(f[1]);
f[1].draw_Flag();

f[2] = make_Russian_flag(f[2]);
f[2].draw_Flag();

f[3] = make_Swedish_flag(f[3]);
f[3].draw_Flag();
}

//////////////////////////////////
/////////////////////////////////

////////////////////////////////
//                            //
//        Nivel 4            //
//                          //
/////////////////////////////

void flags_exchange(Flag f[], int x, int y)
{
Flag m = f[x];
f[x] = f[y];
f[y] = m;
}

void flags_isort(Flag f[], int n)
{
for (int i = 1; i < n; i++)
{
int j = i;
//    int j = n-1;
while (j > 0)
{
flags_exchange(f, j-1, j);
j--;
}
}
}
void update(Flag f[], int n, Coords cr[], float width_flag) {

//int i = 0;

flags_isort(f, n);
for (int i = 0; i < n; i++) {
//f[i] = new Flag(cr[i].x,cr[i].y,width_flag,0,0);
if (f[i].name.equals ( "Islandia")) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, 0, 0);
f[i] = make_Iceland_flag(f[i]);
} else if (f[i].name.equals(  "Italia")) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, 0, 0);
f[i] = make_Italian_flag(f[i]);
} else if (f[i].name.equals( "Russia")) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, 0, 0);
f[i] = make_Russian_flag(f[i]);
} else if (f[i].name.equals( "Suecia")) {
f[i] = new Flag(cr[i].x, cr[i].y, width_flag, 0, 0);
f[i] = make_Swedish_flag(f[i]);
}
//f[i].x = cr[i].x;
//f[i].y = cr[i].y;
}

//  for(int i = 0; i < n; i++){
//print(i+" " + f[i].name+"\n");
// }
// print("\n\n");

//}
}

void flags_draw(Flag f[], int n) {

// f[0] = make_Iceland_flag(f[0]);
//f[1] = make_Italian_flag(f[1]);
// f[2] = make_Russian_flag(f[2]);
// f[3] = make_Swedish_flag(f[3]);

for (int i = 0; i < n; i++) {
f[i].draw_Flag();
print(i+" " + f[i].name+"\n");
}
print("\n\n");
}

void nivel_4() {

background(100);
flags_draw(f, n);
//update(f, n, cr, width_flag);

if (millis() - timer >= 2000) {
//nivel_4();

background(100);
println("hello 1");
timer = millis();
flags_draw(f, n);
update(f, n, cr, width_flag);
}
}

////////////////////////////////

void draw() {

//Nivel 1
//nivel_1();

//Nivel 2
//nivel_2();

//Nivel 3
//nivel_3();

//Nivel 4
//if (millis() - timer >= 2000) {
nivel_4();
//timer = millis();
//nivel_4();
//}
}
``````