Login Screen

edited January 2018 in Questions about Code

I am trying to make a login screen in which there are 2 windows and you enter a username and password and when you click enter it will switch to a second screen. I have gotten this far:

//declare global variables
boolean slides[] = {true, false, false, false, false, false, false, false, false, false, false};
String topText = "";
String bottomText = ""; 
String wasText = "";
String bottomWasText = ""; 
boolean username = true;
void setup() {
  size(600, 600);
}
//Function to draw (every procedure is called here)
void draw() { 
  loginMenu(); 

  tableOfContents();  


}
//Function(Procedure) controls Login Menu. 
void loginMenu() { 
  if (slides[0] == true) {
    background(255);
    rectMode(CENTER); 
    fill(255);
    rect(300, 200, 300, 100);
    rect(300, 400, 300, 100);
    fill(0);
    textSize(20);
    text(topText, 200, 215);
    text(bottomText, 200, 375);
    text(wasText, 200, 215);

  }
} 

//allows keyboard input for various things
void keyPressed() {

  if (slides[0] == true) {

    if ((keyCode == BACKSPACE && topText.length() > 0)) 
    { 
      if (username == true) {
        topText = topText.substring(0, topText.length()-1);
      }
    }
    if ((keyCode == BACKSPACE && bottomText.length() > 0) ) {
      if (username == false) {
        bottomText = bottomText.substring(0, bottomText.length()-1);
      }
    }
      else if (keyCode == ENTER) 
    {           
      username = false;
      wasText = topText;
      bottomWasText = bottomText;
      if (wasText == "username" && bottomWasText == "password") {
           slides[0] = false;
        slides[1] = true; 


      }
    } else if ((key >= 32 && key <= 126))//add char to string
    {
      if (username == true) {
        topText = topText + key;
      } else {
        bottomText = bottomText +key;
      }
    }
  }
  }

void tableOfContents() {
  if (slides[1] == true) {
    background(255); 
    fill(0); 
    rect(250, 250, 30, 30);
  }
}

Why won't it switch to the tableOfContents screen when you type in "username" and "password".

Tagged:

Answers

  • Anybody know how to fix this problem. I am really struggling to fix it. Any help would be appreciated. Thanks.

  • Answer ✓

    Your code is a bit hard to maintain. You will need to create functions that execute specific tasks, or you should consider using objects (oop) as this will be hard to maintain. On a side note, there are libraries out there that can provide you GUI functionality out of the box. If you are doing this as an exercise, consider the changes below (Notice: partial code). There were two bugs. One was related to selecting the password field right after the username field. As soon as it changes to collect the password, your code was evaluating for user+password correctness. The second bug is related to how you were testing this. There is a difference between

    String text="msg";
    if(text=="msg"){
      //Will never be true as it is compering the reference, not the content of your var
    } 
    

    Instead, do this (https://processing.org/reference/String_equals_.html):

      String text="msg";
        if(text.equals("msg")==true){
          println("String values(contents) are the same");
        } 
    

    I hope this helps,

    Kf

    //allows keyboard input for various things
    void keyPressed() {
    
      if (slides[0] == true) {
    
        if ((keyCode == BACKSPACE && topText.length() > 0)) 
        { 
          if (username == true) {
            topText = topText.substring(0, topText.length()-1);
          }
        }
        if ((keyCode == BACKSPACE && bottomText.length() > 0) ) {
          if (username == false) {
            bottomText = bottomText.substring(0, bottomText.length()-1);
          }
        } else if (keyCode == ENTER) {  
    
          if (username==true) {
            username = false;
            wasText = topText;
            return;  //Now ready to start collecting password
          }
          bottomWasText = bottomText;
          if (wasText.equalsIgnoreCase("usr") && bottomWasText.equalsIgnoreCase("pwd")) {
            slides[0] = false;
            slides[1] = true;
          } else println("failed  ", wasText, bottomWasText);
        } else if ((key >= 32 && key <= 126))//add char to string
        {
          if (username == true) {
            topText = topText + key;
          } else {
            bottomText = bottomText +key;
          }
        }
      }
    
      println(topText+"  ++ "+bottomText);
    }
    
  • Thanks man your a genius

Sign In or Register to comment.