What's a good way to save/restore an area?

edited August 2014 in How To...

I'd like to temporarily put a 'dialog box' on the screen to ask the user for some information (from the keyboard). Upon detecting ENTER I would then restore the original pixels. I have searched 'examples' and the forum, but have not yet discovered an answer - sorry if I've missed something obvious. I'd prefer to keep the saved area in memory, but if it's necessary I could save/restore using a temporary file.

Answers

  • Answer ✓

    Dialog boxes w/ JOptionPane:
    http://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html

    /** 
     * No Repeat ID Input (v1.10)
     * by GoToLoop (2013/Nov)
     * 
     * forum.processing.org/two/discussion/869
     * /check-array-contents-with-arraylist
     */
    
    import static javax.swing.JOptionPane.*;
    
    final StringList ids = new StringList( new String[] {
      "Eric", "Beth", "Katniss"
    } 
    );
    
    void draw() {
      println(ids);
    
      final String id = showInputDialog("Please enter new ID");
    
      if (id == null)   exit();
    
      else if ("".equals(id))
        showMessageDialog(null, "Empty ID Input!!!", 
        "Alert", ERROR_MESSAGE);
    
      else if (ids.hasValue(id))
        showMessageDialog(null, "ID \"" + id + "\" exists already!!!", 
        "Alert", ERROR_MESSAGE);
    
      else {
        showMessageDialog(null, "ID \"" + id + "\" successfully added!!!", 
        "Info", INFORMATION_MESSAGE);
    
        ids.append(id);
      }
    }
    
  • Answer ✓

    Upon detecting ENTER I would then restore the original pixels.

    We can get a canvas snapshot via get(): http://processing.org/reference/get_.html

  • Thank You! Great answers! I wasn't aware of showInputDialog (or any of the machinery that goes with it), but it is (almost) the perfect answer for my need (and for another user input problem that just showed up this morning) -- except for the flaw that it doesn't work in 'Present' mode. I notice, from a bit of googling, that you have handed out this solution (including a password variation) for quite a few years, which I missed 'cuz I was searching for 'save/restore area' (not 'dialog box'), thinking that that was where I needed to start (and the get/set descriptions don't mention that concept, so they don't show up in a search).

    I'm amazed at how far I've come along in just a week, starting from ground-zero in terms of knowledge of processing, with (of course!) the help of a great many people who, like you, have offered their knowledge on the web. Thanks again for your help.

Sign In or Register to comment.