How to make multiple screens accessible

edited January 2017 in Questions about Code

So the objective of this project is to make a main menu screen where a user can click either capital A, B, C or lower case or also click on the boxes to get to the sub screens. I can get the screens to show hoever i have problem making them stay can anyone please help me with this in a simple enough way. Thank You

int r=255, g=128, b=128;
boolean keyIsPressed = false;





void setup() {
  size(500, 500);
}

void draw() {
  background(150);
  if (keyIsPressed) {
    background(255, 0, 0);
  }
  if (keyPressed) {
    if (key == 'A' || key == 'a') {
      background(255, 0, 0);
      keyIsPressed = true; 
    } else {
      if (key == 'B' || key == 'b') {
        background(0, 255, 0);
        if(keyIsPressed) {


        if (key =='C' || key == 'c') {
          background(0,0,255);

        }
      }
    }
  }
}
}
Tagged:

Answers

  • edited January 2017

    This is the main screen where everything starts

        void setup() {
          size(500, 500);
        }
    
        void draw() {
          background(128, 0, 128);
          fill(255, 0, 0);
          rect(50, 125, 420, 50);
          fill(0, 255, 0);
          rect(50, 195, 420, 50);
          fill(0, 0, 255);
          rect(50, 265, 420, 50);
          fill(5, 74, 189);
          textSize(25);
          text("A.--------------------", 70, 160); //First Menu option
          fill(138, 11, 238);
          text("B.--------------------", 70, 230); //Second Menu Option
            fill(238, 11, 11);
          text("C.--------------------", 70, 300); 
          fill(0, 255, 255);
          rect(0, 0, 500, 20);
          fill(0, 0, 0);
          textSize(15);
          textAlign(LEFT);
          text("Name", 0, 15); //Title
          textAlign(CENTER);
          text("Screen #1", 250,15);
          textAlign(RIGHT);
          text("Jan 21th 2017", 500, 15);
          fill(200);
          rect(0,475,500,250);
          textSize(14);
          textAlign(LEFT); 
          fill(0,0,0);
          text("Click/ Press A, B, C To Choose A Selection.", 170, 490);
          textSize(15);
          text("x: "+mouseX+" y: "+mouseY, 10, 490);
    
  • Thanks for the help however do you know how to get the screen to change when a mouse is clicked in a specific rectangle using the mouseClicked() function

  • edited January 2017

    If you have an array of instances of class Button, you just need to find out which 1 was clicked inside mousePressed() by iterating each 1 in a loop.

    void mousePressed() {
      for (Button btn : buttons)  if (btn.isMouseOver()) {
        btn.action();
        redraw();
        return;
      }
    }
    

    Once found the clicked Button via isMouseOver(), invoke its custom action() method.

    When instantiating the Button, you just need to @Override its empty action() method using your own implementation. *-:)

    So here's another example just to demo the idea: B-)

    /**
     * Button Chosen Action (v1.0)
     * GoToLoop (2017-Jan-24)
     *
     * forum.Processing.org/two/discussion/20437/
     * how-to-make-multiple-screens-accessible#Item_
     */
    
    static final int BUTTONS = 2, W = 100, H = 60, GAP = 50;
    final Button[] buttons = new Button[BUTTONS];
    
    void setup() {
      size(400, 300);
      noLoop();
      rectMode(CORNER);
    
      stroke(0);
      strokeWeight(1.5);
    
      createButtons();
    }
    
    void draw() {
      background(0350);
      for (Button btn : buttons)  btn.display();
    }
    
    void mousePressed() {
      for (Button btn : buttons)  if (btn.isMouseOver()) {
        btn.action();
        redraw();
        return;
      }
    }
    
    void createButtons() {
      buttons[0] = new Button(GAP, height - H >> 1, W, H, #FF0000) {
        @ Override public void action() {
          println("\nI am button #0\n" + toString());
        }
      };
    
      buttons[1] = new Button(width - W - GAP, height - H >> 1, W, H, #0000FF) {
        @ Override public void action() {
          println("\nI am button #1\n" + toString());
        }
      };
    }
    
    class Button {
      final short x, y, w, h;
      final color c;
    
      Button(int _x, int _y, int _w, int _h, color _c) {
        x = (short) _x;
        y = (short) _y;
        w = (short) _w;
        h = (short) _h;
        c = _c;
      }
    
      void action() {
      }
    
      void display() {
        fill(c);
        rect(x, y, w, h);
      }
    
      boolean isMouseOver() {
        return mouseX > x & mouseX < x+w & mouseY > y & mouseY < y+h;
      }
    
      @ Override String toString() {
        return "x: " + x + ", y: " + y + ", w: " + w + ", h: " + h;
      }
    }
    
Sign In or Register to comment.