How to take input from JOptionPane and place it on canvas

edited April 2017 in How To...

The idea is that the input pane will receive a character or characters, then write it on the canvas in some location. Any ideas on how to execute this?

Tagged:

Answers

  • @kfrajer This is not a duplicate post. The previous asks how to add it to an array, not have it show up on the canvas.

  • pseudocode time, but I have figured it out.

    String x;

    x= JOptionPane.showInputDialog(null, "enter whatever"); char y= x.charAt(0); // converts input from pane to a character

    if (someArray.indexOf(y) >=0)// checks if y is in said array

    { //whatever} if (getWord.indexOf(y) ==#){//where # is the bin number the character is contained in text(y,20,20);}// displays the correct letter, the location

  • Excuse me, is JOptionPane available in processing 3? Does is provide a text are input field? Similar to an editor? Thanks !

    Chrisir

  • edited April 2017 Answer ✓

    @Chrisir -- JOptionPane is part of Java Swing -- it creates a pop-up dialog:

    So you include it at the top of a sketch like this:

    import javax.swing.JOptionPane;
    

    Lately we have been getting a lot of forum questions and posts about it -- I wonder if there is a class somewhere that is requiring it for user input, or a popular video tutorial that uses it.

  • I am still looking for a text area that works under P3D for my 3D turtle

  • Answer ✓

    @Chrisir

    This is not the best but a textfield on P3D from controlP5. Use the >>`<< symbol to clear the text area. You can type and press backspace. You get the scrollbar when you reach the vertical limit of your text area. However, it does not respond to mouse actions like move cursor or selecting text.

    Kf

    /**
     * ControlP5 with PeasyCam support. tested with peasy 0.8.2
     *
     * by jeffg 2011
     */
    
    import peasy.*;
    import controlP5.*;
    import processing.opengl.*;
    
    PeasyCam cam;
    ControlP5 cp5;
    
    int buttonValue = 1;
    
    int myColor = color(255, 0, 0);
    Textarea myTextarea;
    
    
    void setup() {
      size(400, 800, P3D);  //OPENGL
      cam = new PeasyCam(this, 200);
      cp5 = new ControlP5(this);
      cp5.addButton("button", 10, 100, 60, 80, 20).setId(1);
      cp5.addButton("buttonValue", 4, 100, 90, 80, 20).setId(2);
      cp5.setAutoDraw(false);
    
      myTextarea = cp5.addTextarea("txt")
        .setPosition(100, 500)
        .setSize(200, 200)
        .setFont(createFont("arial", 12))
        .setLineHeight(14)
        .setColor(color(128))
        .setColorBackground(color(255, 100))
        .setColorForeground(color(255, 100));
      ;
      myTextarea.setText("Lorem Ipsum is simply dummy text of the printing and typesetting"
        +" industry. Lorem Ipsum has been the industry's standard dummy text"
        +" ever since the 1500s, when an unknown printer took a galley of type"
        +" and scrambled it to make a type specimen book. It has survived not"
        +" only five centuries, but also the leap into electronic typesetting,"
        +" remaining essentially unchanged. It was popularised in the 1960s"
        +" with the release of Letraset sheets containing Lorem Ipsum passages,"
        +" and more recently with desktop publishing software like Aldus"
        +" PageMaker including versions of Lorem Ipsum."
        );
    }
    void draw() {
    
      background(0);
      fill(myColor);
      pushMatrix();
      //translate(150,0,0);
      box(30);
      translate(0, 0, 20);
      fill(0, 0, 255);
      box(5);
      popMatrix();
      // makes the gui stay on top of elements
      // drawn before.
    
      gui();
    }
    
    void gui() {
      hint(DISABLE_DEPTH_TEST);
      cam.beginHUD();
      cp5.draw();
      cam.endHUD();
      hint(ENABLE_DEPTH_TEST);
    }
    
    void controlEvent(ControlEvent theEvent) {
      println(theEvent.getController().getId());
    }
    
    void button(float theValue) {
      myColor = color(random(255), random(255), random(255));
      println("a button event. "+theValue);
    }
    
    void keyPressed() {
    
      //println(hex(key,4) + " . " + keyCode); 
      //backspc  0008 . 8
      //del      007f . 147
      //enter    000a . 10
      //Spc   0020 . 32
      //numLck 0000 . 148
      //home   0000 . 2
      //end    0000 . 3
      //ins    0000 . 26
      //`      0060 . 96
      //up     ffff . 38
      //down   ffff . 40
      //left   ffff . 37
      //right  ffff . 39
      //shift  ffff . 16
      //ctr    ffff . 17
      //alt    ffff . 18
    
      if (key==96) {
        myTextarea.setText("");
      } else if (key==8) {
        int clen=myTextarea.getText().length();
        if (clen>0)
          myTextarea.setText(myTextarea.getText().substring(0, clen-1));
      } else {
        myTextarea.setText(myTextarea.getText()+key);
      }
    }
    
  • Thanks a ton!

    Great!

    I especially need cursor up down left right

    Backspace and delete

    I don't have time today but I will try

    Thank you!

  • Ok... so for my last post... nahh it is an improvement but not enough.

    For key codes... https://forum.processing.org/two/discussion/comment/51840/#Comment_51840

    and the real deal... still to be tested in P3D but it looks promising even if it only works in 2D, check calsign's post:

    https://forum.processing.org/one/topic/text-box-cursor-rendering-improvment.html

    Kf

  • edited April 2017

    @kfrajer:

    I just tried your controlP5 code with my 3D Turtle

    It is not a good textarea though.

    • How would I implement crs up/down / left / right and Home / End in the controlP5 textarea?

    (in a P3D sketch!)

    I haven't tried the other two links but this controlP5 was very frustrating.

    Even my editor is much better than this.

    Chrisir

    ~O)

Sign In or Register to comment.