.

edited May 2015 in How To...

Answers

  • It seems more like a case for app state approach: *-:)

    // forum.processing.org/two/discussion/10674/counting-keypressed-and-using-this
    
    static final int STATES = 4;
    static final int DEFAULT = 0, DISPLAY = 1, COLOUR = 2, WIPE = 3;
    int state = DEFAULT;
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      println(state);
    }
    
    void keyPressed() {
      int k = keyCode;
      if (k == ' ')  state = (state + 1) % STATES;
      redraw = true;
    }
    
  • edited May 2015

    ,

  • edited May 2015

    .

  • edited May 2015

    .

  • edited May 2015

    That was just a template. Inside draw(), check which state is current and do that task.
    Use if/else if block or a switch/case block: :-B

    // forum.processing.org/two/discussion/10674/counting-keypressed-and-using-this
    
    static final int STATES = 4;
    static final int MENU = 0, DISPLAY = 1, COLOUR = 2, WIPE = 3;
    int state = MENU;
    
    void setup() {
      noLoop();
    }
    
    void draw() {
      println(state);
    
      switch (state) {
      case MENU: 
        menu();
        break;
      case DISPLAY: 
        display();
        break;
      case COLOUR: 
        colour();
        break;
      case WIPE: 
        wipe();
      }
    }
    
    void keyPressed() {
      int k = keyCode;
      if (k == ' ')  state = (state + 1) % STATES;
      redraw = true;
    }
    
    void menu() {
    }
    
    void display() {
    }
    
    void colour() {
    }
    
    void wipe() {
    }
    
  • @GoToLoop thank you that was helpful I am a beginner so will try and do what you said, I know what Ifs and elses do, but what do you mean when you say "Use if/else if block or a switch/case block:"

  • noLoop() is for when we want draw() to be called once for each redraw().
    If your app needs to animate things while waiting for user input, it can't use noLoop()/redraw() approach! :-S

  • edited May 2015

    .

  • edited May 2015

    .

  • edited May 2015

    Hmm... Tweaked my template to use your wallpaper now.
    Just add your contrast() & negative() code in those respective functions: O:-)

    // forum.processing.org/two/discussion/10674/counting-keypressed-and-using-this
    // 2015-May-06
    
    static final int STATES = 4;
    static final int ORIGINAL = 0, CONTRAST = 1, NEGATIVE = 2, WIPE = 3;
    int state = ORIGINAL;
    
    PImage img;
    
    void setup() {
      if (img == null)  img = loadImage("http://" + "static.windows8wallpaperhd.com"
        + "/2012/07/1280x720/" + "wallpaper-planet-mars-high-resolution-125.jpg");
      size(img.width, img.height, JAVA2D);
      noLoop();
    }
    
    void keyPressed() {
      int k = keyCode;
      if (k == ' ')  state = (state + 1) % STATES;
      redraw = true;
      frame.setTitle(str(state));
    }
    
    void draw() {
      if (state == WIPE)  clear();
      else                background(img);       
    
      switch (state) {
      case CONTRAST:
        contrast();
        break;
      case NEGATIVE:
        negative();
      }
    }
    
    void contrast() {
    }
    
    void negative() {
    }
    
  • There's also Processing's filter() fx function: :D
    https://processing.org/reference/filter_.html

  • edited May 2015

    .

  • edited May 2015

    .

  • And I've made a bug at the beginning of draw(): =P~
    It's fixed now: :-\"

      if (state == WIPE)  clear();
      else                background(img);
    
  • edited May 2015

    If you haven't done contrast() yet, just leave it empty or remove it completely from the constants.

    ... also why do I need case CONTRAST and void contrast, ...

    CONTRAST is a constant int variable. While contrast() is a function (a.K.a. method).
    Just made them as separate functions for organization purpose, so the switch/case block is left uncluttered.

    And are you sure you don't wanna try out noLoop()/redraw() approach?
    It's much simpler than leaving auto draw() callback. B-)

  • edited May 2015

    .

  • edited May 2015

    Up till now I thought the states changed only after a user pressed space key.
    But now you're saying it is frameCount based!
    Although I don't grasp exactly what you mean by each 20 frames.
    Is it about changing current effect state after 20 seconds? :-/
    1 last thing: Are those effects dynamic or static? That is: gradual or at once?

  • edited May 2015

    .

  • edited May 2015

    Definitely it's not noLoop()/redraw() approach!
    But again, what do you mean by 20 frames? By default, draw() is called @ 60 FPS.
    20 frames would be merely 1/3 of a second! $-)

  • edited May 2015

    .

  • edited May 2015

    If you wanna bind the whole animation to 20 frames, store current frameCount at the animation's start.
    While in draw(), check if it gets equal to stored value + 20. Issue noLoop() if so.
    Of course, have loop() inside keyPressed() in order to re-ignite the animation.

    1. https://processing.org/reference/noLoop_.html
    2. https://processing.org/reference/loop_.html
  • edited May 2015

    .

  • edited May 2015

    .

  • edited April 2016

    Re-adjusted my template for latest knowledge. Using filter() just to have something to do:

    // forum.processing.org/two/discussion/10674/counting-keypressed-and-using-this
    // 2015-May-06
    
    static final int STATES = 4, FRAMES = 20, FPS = 10, SMOOTH = 0;
    static final int ORIGINAL = 0, CONTRAST = 1, NEGATIVE = 2, WIPE = 3;
    
    int state = ORIGINAL, endFrame;
    PImage img;
    
    void setup() {
      if (img == null)  img = loadImage("http://" + "static.windows8wallpaperhd.com"
        + "/2012/07/1280x720/" + "wallpaper-planet-mars-high-resolution-125.jpg");
    
      //size(img.width, img.height, JAVA2D);
      surface.setSize(img.width, img.height);
    
      frameRate(FPS);
      smooth(SMOOTH);
    }
    
    void keyPressed() {
      if (key != ' ')  return;
    
      state = (state + 1) % STATES;
      endFrame = frameCount + FRAMES;
      println(frameCount + ", " + endFrame);
      loop();
    }
    
    void draw() {
      if (frameCount == endFrame)  noLoop();
    
      frame.setTitle("State: " + state + "    Frames: " + frameCount);
    
      switch (state) {
      case ORIGINAL:
        background(img);
        noLoop();
        break;
    
      case CONTRAST:
        contrast();
        break;
    
      case NEGATIVE:
        negative();
        break;
    
      case WIPE:
        wipe();
      }
    }
    
    void contrast() {
      filter(DILATE);
    }
    
    void negative() {
      filter(ERODE);
    }
    
    void wipe() {
      filter(BLUR, 3);
    }
    
  • Hi @GoToLoop ! Great coding! I have a question for you. When I use your code, but try to put in my own coding instead of filter(DILATE);, line 53, an error comes up. "Cannot invoke setTitle(STRING) on the primitive type int"

    Any idea on how to fix this? Thank you :)

  • edited May 2015

    Convert it to a String w/ str() or by concatenating it w/ an empty + "" one:
    https://processing.org/reference/strconvert_.html

    Although I've got no idea how a code inside contrast() would affect the setTitle() at the top of draw(). :-/

Sign In or Register to comment.