Making a queue manager

Hello all. With the help of this forum, I was able to create the first version of my queue manager, One small problem: After 11 names are entered, the names go off screen. I tried to fix it by making another array, making a function to change the X position of the text(), all with no luck.

Here is my code:

int nameStat = 1;

boolean loopStat = false;

int XPos = 20;

int loop = 0;

int s = 0;

int i = 0;

String currentName = "";

int z = i + 1;

String names[] = new String[30];

boolean startupStatus = false;

int numFrames = 166;

PImage[] images = new PImage[numFrames];

int currentFrame = 0;

void setup() {


  imageMode(CENTER);

  size(900, 650);
  String letter = "";
  for (int i = 0; i < 11; i++) names[i] = letter;
  for (int i = 0; i < images.length; i++) {
    //String imageName = "frame" + nf(i, 4) + ".png";
    //Load images
    //images[i] = loadImage(imageName);
  }
  frameRate(29.997);
}


void draw() {
  background(0);
  if (startupStatus == false) {
    //image(images[currentFrame], width / 2, height / 2);
    currentFrame++; //Next Frame
    text("Initializing... TechMaster Industries ServiceTicketManager VbGUI1.3.2", 10, 10);
    if (s>=100) {
      text("Starting Services", 10, 25);
    }
    if (s >=175) {
      text("Acquiring Canadian Cirizenship", 10, 40);
    }
    if (s >=185) {
      text("Error: Trump's Wall is blocking Signal", 10, 55);
    }
    if (s >= 200) {
      text("Loading KeyGen Detector", 10, 70);
    }
    if (s >= 220) {
      text("KeyGen Test Passed", 10, 85);
    }
    if (s >=225) {
      text("Loading Vectors", 10, 100);
    }
    if (s >= 232) {
      text("Loading Sprites", 10, 115);
    }
    if (s >= 240) {
      text("Loading Sprites", 10, 130);
    }
    if (s >=245) {
      text("Loading AI bash scripts", 10, 145);
    }
    if (s >=300) {
      text("Loading Kernel ROM", 10, 160);
    }
    if (s >=325) {
      text("Loading TechMasterOS", 10, 175);
    }
    if (s >= 400) {
      text("Loading Rick Astley", 10, 190);
    }
    if (s >= 420) {
      text("Loading Batch Programs", 10, 205);
    }
    if (s >= 450) {
      text("Extracting TechMaster Files", 10, 220);
    }
    if (s >= 500) {
      text("Extraction Complete", 10, 235);
    }
    if (s >= 505) {
      text("Downloading More RAM", 10, 260);
    }
    if (s >= 515) {
      text("Pirating Adobe Creative Cloud", 10, 275);
    }
    if (s >= 530) {
      text("Getting Board Yet?", 10, 290);
    }
    if (s >= 600) {
      text("Extracting Kevin McCallister", 10, 305);
    }
    if (s >= 650) {
      text("Alerting Kevin's Mom", 10, 320);
    }
    if (s >= 680) {
      text("KEVIN!", 10, 335);
    }
    if (s >= 700) {
      text("U forgot a semi colon", 10, 350);
    }
    if (s >= 750) {
      text("Hi! I'm Cortana", 10, 375);
    }
    if (s >= 790) {
      text("Checking Activation status with Activation Server", 10, 390);
    }
    if (s>=800) {
      text("Activation Succesful. Welcome to TechMaster", 10, 405);
    }
    s++;
    if (currentFrame == images.length && loopStat == false) {
      currentFrame = 0;   
      loopStat = true;
    }
    if (currentFrame == images.length) {
      if (loop == 3) {
        background(0);
        delay(1000);
        startupStatus = true;
        currentName = "";
      } else {
        loop++;
        currentFrame = 0;
        print(loop);
      }
    }
  }

  if (startupStatus == true) {
    background(0);
    textAlign(CENTER);
    textSize(22);
    text("Welcome to the TechMaster Industries TechHelp Desk Please type in your \n name followed by ENTER to reserve a spot in the queue", 450, 50);
    textSize(25);
    text(currentName, 450, 160);
    textAlign(LEFT);
    text("Current Queue (Estimated Wait Time:" + i * 5 + " Minutes)", 10, 250);
    textSize(20);

    for (int i = 0; i < 11; i++) {
      if (names[i] != null ) {
        text(names[i], 20, 300 + i * 35);
      }
    }
  }
  z = i + 1;
  if (i < 0) {
    i=0;
  }
}





void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      names = reverse(names);
      names = shorten(names);
      names = reverse(names);
      //names = shorten(names);
      i--;
    } else if (keyCode == DOWN) {
      s = 800;
      loop = 3;
    }
  }
  if (key == BACKSPACE) {
    currentName = currentName.substring(0, max(0, currentName.length() - 1));
  } else if (key == ENTER) {
    names[i] = currentName;
    currentName = "";
    i = i + 1;
  } else {
    currentName += key;
  }
}

(You can press the DOWN arrow key to skip the nonsense at the beginning, and UP arrow key to remove names)

(The commented image code is for an animation I displayed at the beginning, I don’t think it’s gonna be easy to host these images and have people download them, so I just removed them entirely)

Basically, I’m trying to get it so that after 11 names, the list moves x pixels to the right so that more names can be displayed

Thanks in advance

1 Like

Hi there TechMaster, nice to see you’re working on your own program. Also kind of you to alert Kevin’s mother, that boy is out of control sometimes.

A nice thing about arrays is that it has many build in functionalities which we can use. One of these is retrieving the size of an array:

String[] names = new String[20];
println(names.length);

A downside to your current sketch is that you initialize its size at the start with String names[] = new String[30]. Because you do that the array is filled with 30 empty slots, and therefore you can’t use the .length functionality to its full potential (perhaps there’s an easy way, but I’m not aware of it).

In your situation it might be better to use a Stringlist. It’s a dynamic array, which means you can add and remove slots without too much of a hassle. This would be perfect in your sketch, because you’re not sure how many names a user will add. Using that adding names becomes much easier:

String currentName = "";
StringList names;

void setup() {
  size(900, 650);
  textSize(36);
  names = new StringList();
}

void draw() {
  background(0);
  editName();
  showNames();
}

void editName() {
  fill(250);
  text(currentName, 20, 100);
}

void showNames() {
  int namesPerColumn = 8;
  int yPos = 0;
  translate(20, 200);
  pushMatrix();
  for (int i = 0; i < names.size(); i++) {
    yPos = (i % namesPerColumn) * 35;
    if (i != 0 && i % namesPerColumn == 0) {
      translate(200, 0);
    }
    String name = names.get(i);
    text(name, 0, yPos);
  }
  popMatrix();
}

void keyPressed() {
  if (key == BACKSPACE) {
    currentName = currentName.substring(0, max(0, currentName.length() - 1));
  } else if (key != ENTER) {
    currentName += key;
  }

  if (key == ENTER) {
    names.append(currentName);
    currentName = "";
  }
}

Also I have some other suggestions, of which some are personal preferences:

  • make use of translate instead of hard coding values. J David Eisenberg wrote a great tutorial on it called ā€˜2D Transformations’.
  • you might want to use switches instead for a more organised program flow.
  • Keep your void draw() clean. You could write functions for specific tasks and call these in void draw() instead (in the same way as I used the functions editName() and showNames() in the example above).
  • Prevent global variable names such as int i, because you’re using these in for loops as well. This might cause conflict.
  • You could combine some lines of code of same variable types. For instance, the following:
int loop = 0;
int s = 0;
int i = 0;

is the same as:

int loop = 0, s = 0, i = 0;

Hopefully these suggestions help. If you have any more questions, you know where to find us.

3 Likes

Thank you so much. I’ll begin to implement this in my code now.

1 Like

I tried to do that, but ended up destroying my code. I follow the saying: ā€œIf it ain’t broke don’t fix itā€. But yes I do agree, it would make my code look 10x neater

You should be able to cut & paste without your code breaking, just be wary of not cutting away too many curly brackets. A possible starting point would be to move out everything within the if conditions, so you have a clear overview of your program flow:

void draw() {
  background(0);
  if (startupStatus == false)  page1();
  if (startupStatus == true)   page2();
  z = i + 1;
  if (i < 0) {
    i=0;
  }
}

void page1() {
  // paste code here
}

void page2() {
  // paste code here
}

This is not necessary though, so don’t worry too much about it. It might come in handy however in case your sketch grows bigger :slight_smile:

1 Like

Yeah I think that’s what I did. Either way, thanks so much for the advice. I’m excited to continue learning Processing and the features it has to offer. Coming from Python, this is quite the upgrade

1 Like