Object problem - doesn't work

FluFlu
edited May 2016 in Questions about Code

I went to the Examples page on Processing and found this interesting button that I can press and drag it everywhere. I believe it was at MouseFunctions example. And I took the code, modyfied it a bit, and tried to make an object out of it. But it just wouldn't work...

    void setup() {
      size(640, 360);
    }
    void draw() {
      background(255);
      draggableB Note = new draggableB(width/2,height/2,30);
      Note.move();
      Note.display();
    }

    class draggableB {
      int posX;
      int posY;
      int xOffset;
      int yOffset;
      int size;
      boolean over;
      boolean lock;

      draggableB(int tempX, int tempY, int tempSize) {
        posX = tempX;
        posY = tempY;
        size = tempSize;
      }
      void move() {
        rectMode(CENTER);
        if (mouseX >= posX - size/2 && mouseX <= posX + size/2 && 
          mouseY >= posY - size/2 && mouseY <= posY + size/2) {
          over = true;  
          if (!lock) {
            stroke(0); 
            fill(255, 0, 0);
            size = 30;
          }
        } else {
          stroke(0);
          fill(0, 255, 0);
          size = size - 10;
          over = false;
        }
      }
      void display() {
        rectMode(CENTER);
        rect(posX, posY, size, size);
      }
      void mousePressed() {
        rectMode(CENTER);
        if (over) {
          lock = true; 
          fill(0, 0, 255);
        } else {
          lock = false;
        }
        xOffset = mouseX - posX; 
        yOffset = mouseY - posY;
      }
      void mouseDragged() {
        rectMode(CENTER);
        if (lock) {
          posX = mouseX - xOffset; 
          posY = mouseY - yOffset;
        }
      }

      void mouseReleased() {
        lock = false;
      }
    }

Somedy help me with this please?

«1

Answers

  • line 6 belongs in setup()

  • edited June 2014

    also the mouse functions must be outside the class (write Note.posX then etc.)

    convention: note written small (object)

  • FluFlu
    edited June 2014

    Thanks, but I thought objects have the first letter uppercase to differenciate them from other variable types. And if I'm going to put the mouse functions outside the class am I not gonna have to make another variable for every object like this? Plus I need to declare all the variables outside the class...so another variables for every object again.. This is what I am trying to avoid, that's why I made the class in the first place. Show me how to do that please and maybe it is better the way you say.

  • the class name class draggableB should have first letter uppercase

    but the object, derived from it, small first letter

  • edited June 2014

    the mouse functions won't work in the class anyway (they are called by processing automatically but only outside your class) so you need to take them out of the class anyway

    but surely you don't have to have the vas outside the class

  • edited June 2014

    this works (for one note)

    draggableB Note;
    
    void setup() {
      size(640, 360);
      Note = new draggableB(width/2, height/2, 30);
    } // func 
    
    void draw() {
      background(255);
      Note.move();
      Note.display();
    } // func 
    
    // --------------------------------------------------------
    
    
    void mousePressed() {
      rectMode(CENTER);
      if (Note.over) {
        Note.lock = true; 
        fill(0, 0, 255);
      } 
      else {
        Note.lock = false;
      }
      Note.xOffset = mouseX - Note.posX; 
      Note.yOffset = mouseY - Note.posY;
    } // func 
    
    void mouseDragged() {
      rectMode(CENTER);
      if (Note.lock) {
        Note.posX = mouseX - Note.xOffset; 
        Note.posY = mouseY - Note.yOffset;
      }
    } // func  
    
    void mouseReleased() {
      Note.lock = false;
    } // func 
    
    // =======================================
    
    class draggableB {
      int posX;
      int posY;
      int xOffset;
      int yOffset;
      int size;
      boolean over;
      boolean lock;
    
      draggableB(int tempX, int tempY, int tempSize) {
        posX = tempX;
        posY = tempY;
        size = tempSize;
      }
      void move() {
        rectMode(CENTER);
        if (mouseX >= posX - size/2 && mouseX <= posX + size/2 && 
          mouseY >= posY - size/2 && mouseY <= posY + size/2) {
          over = true;  
          if (!lock) {
            stroke(0); 
            fill(255, 0, 0);
            size = 30;
          }
        } 
        else {
          stroke(0);
          fill(0, 255, 0);
          // size = size - 10;
          over = false;
        }
      }
      void display() {
        rectMode(CENTER);
        rect(posX, posY, size, size);
      }
      //
    } // class 
    //
    
  • So, anyway of doing this, but using objects and few variables it is possible?

  • edited June 2014

    yes, you can use an array of type draggableB

    (or an ArrayList)

    ;-)

  • Answer ✓

    this is a simple array

    draggableB[] notes = new draggableB[12];
    
    void setup() {
      size(640, 360);
      for (int i = 0; i < notes.length; i++) 
        notes[i] = new draggableB(40+i*40, height/2, 30);
    } // func 
    
    void draw() {
      background(255);
      for (int i = 0; i < notes.length; i++) {
        notes[i].move();
        notes[i].display();
      }
    } // func 
    
    // --------------------------------------------------------
    
    
    void mousePressed() {
      rectMode(CENTER);
      for (int i = 0; i < notes.length; i++) {
        if (notes[i].over) {
          notes[i].lock = true; 
          fill(0, 0, 255);
          notes[i].xOffset = mouseX - notes[i].posX; 
          notes[i].yOffset = mouseY - notes[i].posY;
        } 
        else {
          notes[i].lock = false;
        }
      }
    } // func 
    
    void mouseDragged() {
      rectMode(CENTER);
      for (int i = 0; i < notes.length; i++) {
        if (notes[i].lock) {
          notes[i].posX = mouseX - notes[i].xOffset; 
          notes[i].posY = mouseY - notes[i].yOffset;
        }
      }
    } // func  
    
    void mouseReleased() {
      for (int i = 0; i < notes.length; i++) {
        notes[i].lock = false;
      }
    } // func 
    
    // =======================================
    
    class draggableB {
      int posX;
      int posY;
      int xOffset;
      int yOffset;
      int size;
      boolean over;
      boolean lock;
    
      draggableB(int tempX, int tempY, int tempSize) {
        posX = tempX;
        posY = tempY;
        size = tempSize;
      } // constr
    
      void move() {
        rectMode(CENTER);
        if (mouseX >= posX - size/2 && mouseX <= posX + size/2 && 
          mouseY >= posY - size/2 && mouseY <= posY + size/2) {
          over = true;  
          if (!lock) {
            stroke(0); 
            fill(255, 0, 0); // red
            size = 30;
          }
        } 
        else {
          stroke(0);
          fill(0, 255, 0); // green 
          // size = size - 10; // ???
          over = false;
        }
      }
    
      void display() {
        rectMode(CENTER);
        if (lock)
          fill(0, 0, 255); 
        rect(posX, posY, size, size);
      }
      //
    } // class 
    //
    
  • edited June 2014

    I thought objects have the first letter uppercase to differentiate them from other variable types.

    • In Java all variables (except constants) and method names follow the lower camel-case naming convention: https://en.wikipedia.org/wiki/Naming_convention_(programming)#Java

    • Java has 8 keywords to represent its 8 primitive types:
      http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    • Everything else comes from class & interface names.

    • Class & interface names follow the upper camel-case naming convention instead.

    • Objects are instantiated from a class. A contiguous block of memory is allocated w/ enough room to clone all non-static fields and other small details from a class into the object.

    • The 1st memory address of that block is called reference or pointer to the object.

    • Non-primitive variables store that reference value in order to track an object and access its members
      through the dot operator:
      http://processing.org/reference/dot.html

    • So don't mix up variables & objects. Even though non-primitive variables act as an object avatar,
      they're not the same!

    • Variables, constants, methods, classes and interfaces got names.
      Objects are nameless! They only got their reference in order to be reached!

  • Answer ✓

    here is a new improved version

    DraggableB[] notes = new DraggableB[12];
    int selected; // while dragging this is the index 
    
    void setup() {
      size(640, 360);
      rectMode(CENTER);
      for (int i = 0; i < notes.length; i++) { 
        notes[i] = new DraggableB(40+i*40, height/2, 30);
      } // for
    } // func 
    
    void draw() {
      background(255);
      for (int i = 0; i < notes.length; i++) {
        notes[i].move();
        notes[i].display();
      } // for
    } // func 
    
    // --------------------------------------------------------
    
    void mousePressed() {
      for (int i = 0; i < notes.length; i++) {
        if (notes[i].over) {
          notes[i].lock = true; 
          notes[i].xOffset = mouseX - notes[i].posX; 
          notes[i].yOffset = mouseY - notes[i].posY;
          selected=i; // store index
          return;
        } 
        else {
          notes[i].lock = false;
        }
      } // for
    } // func 
    
    void mouseDragged() {
      if (notes[selected].lock) {
        notes[selected].posX = mouseX - notes[selected].xOffset; 
        notes[selected].posY = mouseY - notes[selected].yOffset;
      }
    } // func  
    
    void mouseReleased() {
      notes[selected].lock = false;
    } // func 
    
    // =======================================
    
    class DraggableB {
      int posX;
      int posY;
      int xOffset;
      int yOffset;
      int size;
      boolean over;
      boolean lock;
      int halfSize; 
    
      DraggableB(int tempX, int tempY, int tempSize) {
        posX = tempX;
        posY = tempY;
        size = tempSize;
        halfSize = size/2;
      } // constr
    
      void move() {
        // does not move but rather update
        if (mouseX >= posX - halfSize && mouseX <= posX + halfSize && 
          mouseY >= posY - halfSize && mouseY <= posY + halfSize) {
          over = true;  
          if (!lock) {
            stroke(0); 
            fill(255, 0, 0); // red
            size = 30;
          } 
          else {
            fill(0, 0, 255); // blue
          } // else
        } // if  
        else {
          stroke(0);
          fill(0, 255, 0); // green 
          // size = size - 10; // ???
          over = false;
        } //else
      } // method
    
      void display() {
        rect(posX, posY, size, size);
      } // method
      //
    } // class 
    // 
    
  • Thank you a lot Chrisir! And GoToLoop for the convention for the variables! Now a new question rises. How can I use native UI elements like a text box? Cause to write that it would take me a few good days and I don't have the time right now. The deadline for my app is on 13 of July, so I need to make a text box for every single note paper I just made... and after that, I need to modify them all to look like real notes and make the other functions, but I need to do so many and unfortunately, I didn't quite developed the "programmer's thinking method", if you know what I mean... I searched all over google for native windows text boxes to use them in processing but I couldn't find any... nor a library... Can you please help me with this one.. I feel though that I'm asking you to do too much and I don't have a method to repay you back...

  • Go to main site, to the Libraries page, go to the GUI section, choose a library, there are at least 3 or 4 GUI libraries.

  • edited June 2014 Answer ✓

    here is a new version

    I don't have time to work further on it

    you can

    • add new notes with n
    • edit a note with e (the one your mouse is currently over)
    • delete a note with DEL (the one your mouse is currently over)

      import static javax.swing.JOptionPane.*;
      
      ArrayList<DraggableB> notes = new ArrayList();
      int selected; // while dragging this is the index
      String strInput;
      
      int xPosForNewNote = 40;
      int yPosForNewNote = 70;
      
      void setup() {
        size(640, 360);
        rectMode(CENTER);
        //  for (int i = 0; i < notes.length; i++) { 
        //    currNote = new DraggableB(40+i*40, height/2, 30);
        //  } // for
      } // func 
      
      void draw() {
        background(255);
        fill(0);
        text("Note Manager", width/2, 18);
        text ( "Please hit n for a new note. " + 
          "Hit e to edit node with mouse over (red node), " + 
          "del to delete.", 12, height-18 );
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          currNote.update();
          currNote.display();
        } // for
        //
      } // func 
      
      // --------------------------------------------------------
      
      void mousePressed() {
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          if (currNote.over) {
            currNote.lock = true; 
            currNote.xOffset = mouseX - currNote.posX; 
            currNote.yOffset = mouseY - currNote.posY;
            selected=i; // store index
            return;
          } 
          else {
            currNote.lock = false;
          }
        } // for
      } // func 
      
      void mouseDragged() {
        if (notes.get(selected).lock) {
          notes.get(selected).posX = mouseX - notes.get(selected).xOffset; 
          notes.get(selected).posY = mouseY - notes.get(selected).yOffset;
        }
      } // func  
      
      void mouseReleased() {
        notes.get(selected).lock = false;
      } // func 
      
      // -----------------------------------------------
      
      void keyPressed () {
        DraggableB currNote;
        switch (key) {
        case 'n':
          strInput = showInputDialog("Please enter new text");
          currNote = new DraggableB(xPosForNewNote, yPosForNewNote, 60, strInput);
          notes.add( currNote );
          xPosForNewNote += 80; 
          if ( xPosForNewNote > width-40) {
            xPosForNewNote = 50;
            yPosForNewNote += 80;
          }
          break;
        case 'e':
          // 
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) {
              strInput = showInputDialog("Please enter new text", currNote.text);
              currNote.text=strInput;
              return;
            }
          }
          break; 
        case DELETE:
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) {
              notes.remove(i); 
              return;
            }
          }
          break; 
        default:
          // DO nothing 
          break;
        } // switch
      } // func 
      
      // =======================================
      
      class DraggableB {
        int posX;
        int posY;
        int xOffset;
        int yOffset;
      
        int size;
        int halfSize;
      
        boolean over;
        boolean lock;
      
        String text=""; 
      
        DraggableB(int tempX, int tempY, 
        int tempSize, 
        String tempText) {
          posX = tempX;
          posY = tempY;
          size = tempSize;
          halfSize = size/2;
          text = tempText;
        } // constr
      
        void update() {
          // update
          if (mouseX >= posX - halfSize && mouseX <= posX + halfSize && 
            mouseY >= posY - halfSize && mouseY <= posY + halfSize) {
            over = true;  
            if (!lock) {
              stroke(0); 
              fill(255, 0, 0); // red
              //size = 30;
            } 
            else {
              fill(0, 0, 255); // blue
            } // else
          } // if  
          else {
            stroke(0);
            fill(255, 255, 0); // yellow 
            // size = size - 10; // ???
            over = false;
          } //else
        } // method
      
        void display() {
          noStroke();
          rect(posX, posY, size, size, 
          0, 0, 23, 0);
          fill(0); 
          text(text, posX+2, posY+2, size, size);
        } // method
        //
      } // class 
      //
      
  • Answer ✓

    new version with 2 local menus - use right mouse

    import javax.swing.JOptionPane.* ;
    
    ArrayList<DraggableB> notes = new ArrayList();
    int selected; // while dragging this is the index
    String strInput;
    
    int xPosForNewNote = 40;
    int yPosForNewNote = 70;
    
    // this is a self made local menu without lib 
    // 
    ArrayList<LocalMenu> menus = new ArrayList();
    
    boolean menuOpen = false; 
    int  menuOpenNumber = -1;
    
    // ----------------------------------------------------
    
    void setup() {
    
      size(640, 360);
      rectMode(CENTER);
    
      //  for (int i = 0; i < notes.length; i++) { 
      //    currNote = new DraggableB(40+i*40, height/2, 30);
      //  } // for
    
      ArrayList<LineInTheLocalMenu> lines = new ArrayList();  
      //
      // make menu 1
      lines.clear();
      lines.add( new LineInTheLocalMenu ( "Size  ", "Size: not done", 0 ));
      lines.add( new LineInTheLocalMenu ( "Edit Text ", "Edit the text of the note", 1 ));
      lines.add( new LineInTheLocalMenu ( "Delete", "Delete the note", 2 ));
      lines.add( new LineInTheLocalMenu ( "Cancel ", "Close the menu", 3 ));
      LocalMenu test = new LocalMenu(333.0, 133.0, "Menu for a Note", "Explanation on the menu", lines);
      menus.add( test );
      test = null;
    
      // make menu 2
      lines = new ArrayList();  
      lines.clear();
      lines.add( new LineInTheLocalMenu ( "New Note ", "This creates a new note.", 0));
      LocalMenu test2 = new LocalMenu(93, 233, "Menu for a new note ", "create a new note", lines); 
      menus.add(test2);
      test2 = null;
    
      //
    } // func 
    
    void draw() {
      background(255);
      fill(0);
      text("Note Manager", width/2, 18);
      text ( "Please use right mouse on empty space " + 
        "or on a note.", 12, height-18 );
      for (int i = 0; i < notes.size(); i++) {
        DraggableB currNote = notes.get(i);
        currNote.update();
        currNote.display();
      } // for
      //
      // show menu when necessary 
      if (menuOpen) {
        LocalMenu myMenu = menus.get(menuOpenNumber);
        myMenu.display();
      } // if
      //
    } // func 
    
    // --------------------------------------------------------
    
    void mousePressed() {
      //
      if (menuOpen) {
        mousePressedForMenuOpen ();
      }
      else {
        mousePressedForMenuNotOpen ();
      }
      //
    } // func 
    
    void mouseDragged() {
      if (selected!=-1 && selected < notes.size()) {
        if (notes.get(selected).lock) {
          notes.get(selected).posX = mouseX - notes.get(selected).xOffset; 
          notes.get(selected).posY = mouseY - notes.get(selected).yOffset;
        }
      }
    } // func  
    
    void mouseReleased() {
      if (selected!=-1 && selected < notes.size()) {
        notes.get(selected).lock = false;
      }
    } // func 
    
    // ---------------------------------------------------
    
    void mousePressedForMenuOpen () {
      // menu is open 
      if (mouseButton==RIGHT) {
        // right-click: open another (or same) local menu
        mousePressedForMenuNotOpen ();
      } // if 
      else if (mouseButton==LEFT) {
        // left-click:
        LocalMenu myMenu = menus.get(menuOpenNumber);
        int command = myMenu.overWhichLine();
        if (command!=-1) {
          menuOpen=false;
          doCommand(command);
        }
        else
        {
          // no command identified 
          // close the menu 
          menuOpen=false;
        }
      } // else
    } // func 
    //
    //
    void mousePressedForMenuNotOpen () {
      // menu is not open
      // open a menu if necessary 
      if (mouseButton==RIGHT) {
        menuOpenNumber = -1;
    
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          if (currNote.over) {
            menuOpenNumber=0;
            selected = i;
          }
        } // for
    
        // did he find no item?
        if (menuOpenNumber == -1) {
          menuOpenNumber=1;
          selected = -1;
        }
    
        //  
        // did he find an item?
        if (menuOpenNumber > -1) { 
          menuOpen=true;
          LocalMenu myMapPoint = menus.get(menuOpenNumber);
          myMapPoint.pos.x = mouseX;
          myMapPoint.pos.y = mouseY;
        } // if
        else 
        {
          menuOpen=false;
        }
      } // if (mouseButton==RIGHT) {
      else 
      {
        // LEFT mouse 
        menuOpen=false;
    
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          if (currNote.over) {
            currNote.lock = true; 
            currNote.xOffset = mouseX - currNote.posX; 
            currNote.yOffset = mouseY - currNote.posY;
            selected=i; // store index
            return;
          } 
          else {
            currNote.lock = false;
          }
        } // for
      } // else
      //
    } // func 
    
    // -----------------------------------------------
    
    void keyPressed () {
      if (menuOpen) {
        key=0;
      }
      else 
      {
        DraggableB currNote;
        switch (key) {
        case 'n':
          //
          strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text");
          currNote = new DraggableB(xPosForNewNote, yPosForNewNote, 60, strInput);
          notes.add( currNote );
          xPosForNewNote += 80; 
          if ( xPosForNewNote > width-40) {
            xPosForNewNote = 50;
            yPosForNewNote += 80;
          }
          break;
        case 'e':
          // 
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) {
              strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text", currNote.text);
              currNote.text=strInput;
              return;
            }
          }
          break; 
        case DELETE:
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) {
              notes.remove(i); 
              return;
            }
          }
          break; 
        default:
          // DO nothing 
          break;
        } // switch
      } // else  
      //
    } // func 
    
    // ------------------------------------------------------
    
    void doCommand(int command) {
      final int menu0 = 0;
      final int menu1 = 1;
      final int menu2 = 2;
      //
      switch (menuOpenNumber) {
      case menu0:
        handleMenu0 (command) ;
        break;
      case menu1:
        handleMenu1 (command) ;
        break;
      case menu2:
        handleMenu2 (command) ;
        break;
      default:
        println ("unknown menu");
        break;
      } // switch
    } // func 
    
    //
    void handleMenu0 (int command) {
      // local menu on a note
      switch (command) {
      case 0:
        println (" 0 : 0 " );
        break;
      case 1:
        println (" 0 : 1 " );
        strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text", notes.get(selected).text);
        notes.get(selected).text=strInput;
        break;
      case 2:
        println (" 0 : 2 " );
        notes.remove(selected); 
        break;
      case 3:
        println (" 0 : 3 " );
        menuOpen = false;
        menuOpenNumber = -1; 
        selected = -1; 
        break;
      default:
        break;
      } // inner switch
    }
    
    //
    void handleMenu1 (int command) {
      // local menu on empty space
      switch (command) {
      case 0:
        println (" 1 : 0 " );
    
        menuOpen=false; 
        strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text");
        DraggableB currNote = new DraggableB(mouseX, mouseY, 60, strInput);
        notes.add( currNote );
        xPosForNewNote += 80; 
        if ( xPosForNewNote > width-40) {
          xPosForNewNote = 50;
          yPosForNewNote += 80;
        } // if 
    
        break;
      case 1:
        println (" 1 : 1 " );
        break;
      case 2:
        println (" 1 : 2 " );
        break;
      case 3:
        println (" 1 : 3 " );
        break;
      case 4:
        println (" 1 : 4 " );
        break;
      default:
        break;
      } // inner switch
    }
    
    //
    void handleMenu2 (int command) {
      switch (command) {
      case 0:
        println (" 2 : 0 " );
        break;
      case 1:
        println (" 2 : 1 " );
        break;
      case 2:
        println (" 2 : 2 " );
        break;
      default:
        break;
      } // inner switch
    }
    // 
    // ----------------------------------------------------------------
    //
    void showStatusBar ( String myText ) {
      fill(0);
      rect(0, height-30, 200, 30);
      fill(255);
      text (myText, 20, height-8);
    }
    
    // =======================================
    
    class DraggableB {
      int posX;
      int posY;
      int xOffset;
      int yOffset;
    
      int size;
      int halfSize;
    
      boolean over;
      boolean lock;
    
      String text=""; 
    
      DraggableB(int tempX, int tempY, 
      int tempSize, 
      String tempText) {
        posX = tempX;
        posY = tempY;
        size = tempSize;
        halfSize = size/2;
        text = tempText;
      } // constr
    
      void update() {
        // update
        if (mouseX >= posX - halfSize && mouseX <= posX + halfSize && 
          mouseY >= posY - halfSize && mouseY <= posY + halfSize) {
          over = true;  
          if (!lock) {
            stroke(0); 
            fill(255, 0, 0); // red
            //size = 30;
          } 
          else {
            fill(0, 0, 255); // blue
          } // else
        } // if  
        else {
          stroke(0);
          fill(255, 255, 0); // yellow 
          // size = size - 10; // ???
          over = false;
        } //else
      } // method
    
      void display() {
        noStroke();
        rect(posX, posY, size, size, 
        0, 0, 23, 0);
        fill(0); 
        text(text, posX+2, posY+2, size, size);
      } // method
      //
    } // class 
    // 
    // =====================================
    
    class LocalMenu {
      //
      PVector pos = new PVector();
      PVector size = new PVector();
      String name;
      String textMouseOver;
      ArrayList<LineInTheLocalMenu> lines = new ArrayList();  
      //
      // constr
      LocalMenu ( 
      float x_, float y_, 
      String name_, 
      String textMouseOver_, 
      ArrayList<LineInTheLocalMenu> lines_ ) {
        pos.x=x_;
        pos.y=y_;
        name=name_;
        textMouseOver=textMouseOver_;
        lines=lines_;
        //
        // get the size of this menu
        float myWidth= textWidth (name) ;
        int i = 0; 
        for (LineInTheLocalMenu oneLine : lines ) {
          if (myWidth<textWidth (oneLine.content)) 
            myWidth = textWidth (oneLine.content) ;
          i++;
        }
        size.x = myWidth+15;
        size.y = 15*i+15+15+4; // to do
        //
      } // constr
      //
      void display() {
        // 1st: rect 
        rectMode(CORNER);
        noStroke();
        fill (121);
        rect(pos.x, pos.y, size.x, size.y );
        // 2nd: headline 
        fill(0);
        text(name, pos.x+15, pos.y+15);
        // 3rd: normal menu lines 
        int i=0;
        boolean doneStatusBar = false;
        for (LineInTheLocalMenu oneLine : lines ) {
          fill(245);
          text(oneLine.content, pos.x+15, pos.y +15*i+15+15+4); // to do 
          i++;
          oneLine.pos.x = pos.x+1;
          oneLine.pos.y = pos.y+15*i-29+15+15+4;    // to do
          if (oneLine.mouseOver() && mouseX < pos.x+size.x  && !doneStatusBar )
          {
            oneLine.over = true; 
            showStatusBar ( oneLine.textMouseOver );
            doneStatusBar=true;
          }
          else 
          {
            oneLine.over = false;
          }
        }
        if (!doneStatusBar) {
          if (mouseOver() ) {
            showStatusBar (  textMouseOver  );
            doneStatusBar=true;
          }
        }
        rectMode(CENTER);
      } // method
    
      boolean mouseOver() {
        if (mouseX>pos.x && 
          mouseY>pos.y && 
          mouseX<pos.x+size.x &&
          mouseY<pos.y+size.y ) {
          return true;
        }
        return false;
      } // method
    
      //
    
      int overWhichLine() {
        int i=0;
        for (LineInTheLocalMenu oneLine : lines ) {
          fill(245);
          // text(oneLine.content, pos.x+15, pos.y +15*i+15+15+4); // to do 
          i++;
          oneLine.pos.x = pos.x+1;
          oneLine.pos.y = pos.y+15*i-29+15+15+4;    // to do
          if (oneLine.mouseOver() && mouseX < pos.x+size.x )
          {
            oneLine.over = true; 
            return oneLine.commandID;
          }
        } // for
        return -1;
      } // func 
    
      //
    } // class
    
    // ===============================================================
    
    class LineInTheLocalMenu {
    
      PVector pos = new PVector();
      String content;
      String textMouseOver;
      boolean over = false; 
      int commandID=0;
      // 
      // constr
      LineInTheLocalMenu ( String content_, String textMouseOver_, int commandID_ ) {
        content=content_;
        textMouseOver=textMouseOver_;
        commandID = commandID_;
      }// constr
      //
      boolean mouseOver() {
        if (mouseX>pos.x && 
          mouseY>pos.y && 
          mouseY<pos.y + 19 ) {
          return true;
        }
        return false;
      } // method
      //
    } // class 
    
    // ==============================================
    
  • So, basically you just made like 70% of my app :) Thank you so much, but... you are awesome and awesome, a million times awesome. I couldn't make such a great app by my own. I just have no words to thank you! All I need to do know is to understand all the code (or at least 70% of it) and redesign everything (I need a very good design, I and I got some really good ideas), add some features (a big chance not to make them work) and I'm done! Anyway, I kind of feel bad that I'm gonna present myself with an app that most of it I didn't make, but the whole idea of the contest is originality, and it says that the app should be at least partially functionable, so I think I am good at that point. Anyway, thank you again, Chrisir, I am gonna go to work more at the app now. I've already been working at it for like three-four weeks, a few hours every day, but I'm just not a good programmer like you are...sorry for wasting your time. After finishing it, I will post it at Share your Work, is that ok?

  • wait, I got a new version in stock

    wanna see it?

  • edited June 2014

    here... this version comes with 3 different menus ...

    import javax.swing.JOptionPane.* ;
    
    final String title = "Note Manager";
    final String statusBarText= "Please use right mouse on empty space " + 
    "or on a note. Use left mouse button on main menu.";
    
    ArrayList<DraggableB> notes = new ArrayList();
    int selected; // while dragging this is the index
    String strInput;
    
    int xPosForNewNote = 40;
    int yPosForNewNote = 70;
    
    // this is a self made local menu without lib 
    // 
    ArrayList<LocalMenu> menus = new ArrayList();
    
    boolean menuOpen = false; 
    int  menuOpenNumber = -1; // which menu is open 
    
    // ----------------------------------------------------
    // main funcs 
    
    void setup() {
    
      size(640, 360);
      rectMode(CENTER);
    
      // make menu 1: local menu on existing note
      ArrayList<LineInTheLocalMenu> lines = new ArrayList();  
      lines.add( new LineInTheLocalMenu ( "Size  ", "Size ", 0 ));
      lines.add( new LineInTheLocalMenu ( "Edit Text ", "Edit the text of the note", 1 ));
      lines.add( new LineInTheLocalMenu ( "Delete", "Delete the note", 2 ));
      lines.add( new LineInTheLocalMenu ( "Cancel ", "Close the menu", 3 ));
      LocalMenu test = new LocalMenu(333.0, 133.0, "Menu for a Note", "Menu to change a Note", lines);
      menus.add( test );
      test = null;
    
      // make menu 2: local menu on empty space 
      lines = new ArrayList();  
      lines.add( new LineInTheLocalMenu ( "New Note ", "This creates a new note.", 0));
      lines.add( new LineInTheLocalMenu ( "Cancel ", "Close the menu", 1 ));
      LocalMenu test2 = new LocalMenu(93, 233, "Menu for a new note ", "create a new note", lines); 
      menus.add(test2);
      test2 = null;
    
      // make menu 3: main menu at menu symbol (=)
      lines = new ArrayList();  
      lines.add( new LineInTheLocalMenu ( "New Sheet  ", "This creates a new sheet (deletes all).", 0));
      lines.add( new LineInTheLocalMenu ( "New Note  ", "This creates a new note.", 1));
      lines.add( new LineInTheLocalMenu ( "Make stack  ", "place all notes on a stack.", 2));
      lines.add( new LineInTheLocalMenu ( "Spread notes  ", "Spread all notes ", 3));
      lines.add( new LineInTheLocalMenu ( "Cancel", "Close the menu", 4));
      LocalMenu test3 = new LocalMenu(30, 30, "Main Menu", "The main menu", lines); 
      menus.add(test3);
      test3 = null;
      //
    } // func 
    
    void draw() {
      background(255);
    
      fill(0);
      textAlign(CENTER);
      text(title, width/2, 18);
    
      textAlign(LEFT);
      menuSign(30, 5);
      showStatusBar(statusBarText);
    
      // show notes 
      for (int i = 0; i < notes.size(); i++) {
        DraggableB currNote = notes.get(i);
        currNote.update();
        currNote.display();
      } // for
    
      // show menu when necessary 
      if (menuOpen) {
        LocalMenu myMenu = menus.get(menuOpenNumber);
        myMenu.display();
      } // if
      //
    } // func 
    
    // --------------------------------------------------------
    // Inputs mouse 
    
    void mousePressed() {
      //
      if (menuOpen) {
        mousePressedForMenuOpen ();
      }
      else {
        mousePressedForMenuNotOpen ();
      }
      //
    } // func 
    
    void mouseDragged() {
      if (selected!=-1 && selected < notes.size()) {
        if (notes.get(selected).lock) {
          notes.get(selected).posX = mouseX - notes.get(selected).xOffset; 
          notes.get(selected).posY = mouseY - notes.get(selected).yOffset;
        }
      }
    } // func  
    
    void mouseReleased() {
      if (selected!=-1 && selected < notes.size()) {
        notes.get(selected).lock = false;
      }
    } // func 
    
    // ---------------------------------------------------
    
    void mousePressedForMenuOpen () {
      // menu is open 
      if (mouseButton==RIGHT) {
        // right-click: open another (or same) local menu
        mousePressedForMenuNotOpen ();
      } // if 
      else if (mouseButton==LEFT) {
        // left-click:
        LocalMenu myMenu = menus.get(menuOpenNumber);
        int command = myMenu.overWhichLine();
        if (command!=-1) {
          menuOpen=false;
          doCommand(command);
        }
        else
        {
          // no command identified 
          // close the menu 
          menuOpen=false;
        }
      } // else
    } // func 
    //
    //
    void mousePressedForMenuNotOpen () {
      // menu is not open
      // open a menu if necessary 
      if (mouseButton==RIGHT) {
        menuOpenNumber = -1;
    
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          if (currNote.over) {
            menuOpenNumber=0;
            selected = i;
          }
        } // for
    
        // did he find no item?
        if (menuOpenNumber == -1) {
          menuOpenNumber=1;
          selected = -1;
        }
    
        //  
        // did he find an item?
        if (menuOpenNumber > -1) { 
          menuOpen=true;
          LocalMenu myMapPoint = menus.get(menuOpenNumber);
          myMapPoint.pos.x = mouseX;
          myMapPoint.pos.y = mouseY;
        } // if
        else 
        {
          menuOpen=false;
        }
      } // if Button RIGHT
      else 
      {
        // LEFT mouse 
        menuOpen=false;
    
        if (mouseX>26&&mouseX<60&&
          mouseY>0&&mouseY<30) {
          menuOpen=true; 
          menuOpenNumber=2;
          selected = -1;
        }// if 
    
        else {
    
          // note: start dragging 
          for (int i = 0; i < notes.size(); i++) {
            DraggableB currNote = notes.get(i);
            if (currNote.over) {
              currNote.lock = true; 
              currNote.xOffset = mouseX - currNote.posX; 
              currNote.yOffset = mouseY - currNote.posY;
              selected=i; // store index
              return;
            } 
            else {
              currNote.lock = false;
            }
          } // for
        } // else
      } // else LEFT mouse 
      //
    } // func 
    
    // -----------------------------------------------
    // Inputs keyboard 
    
    void keyPressed () {
      if (menuOpen) {
        // kill key (in case its escape key)
        key=0;
      }
      else 
      {
        DraggableB currNote;
        switch (key) {
        case 'n':
          // make a new note 
          newNote(); 
          break;
        case 'e':
          // edit a note
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) {
              strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text", currNote.text);
              currNote.text=strInput;
              return;
            }
          }
          break; 
    
        case '+':
          // size +
          selected=-1;
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) 
              selected=i;
          }
          if (selected!=-1 && selected < notes.size()) {
            notes.get(selected).size++;
          }
          break;
    
        case '-':
          // size - 
          selected=-1;
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) 
              selected=i;
          }
          if (selected!=-1 && selected < notes.size()) {
            if (notes.get(selected).size>8)
              notes.get(selected).size--;
          }
          break;
    
        case DELETE:
          // delete note 
          for (int i = 0; i < notes.size(); i++) {
            currNote = notes.get(i);
            if (currNote.over) {
              notes.remove(i); 
              return;
            }
          }
          break; 
        default:
          // Do nothing 
          break;
        } // switch
      } // else  
      //
    } // func 
    
    // ------------------------------------------------------
    // menu management 
    
    void doCommand(int command) {
    
      // handles the command from a menu
    
      final int menu0 = 0;
      final int menu1 = 1;
      final int menu2 = 2;
      //
      // for the different menus the lines (commands)
      // are different: 
      switch (menuOpenNumber) {
      case menu0:
        handleMenu0(command);
        break;
      case menu1:
        handleMenu1(command);
        break;
      case menu2:
        handleMenu2(command);
        break;
      default:
        println ("unknown menu");
        break;
      } // switch
    } // func 
    
    //
    void handleMenu0 (int command) {
      // local menu on a note
      switch (command) {
      case 0:
        println (" 0 : 0 " );
        notes.get(selected).size+=20; 
        break;
      case 1:
        println (" 0 : 1 " );
        strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text", notes.get(selected).text);
        if (strInput!=null) { 
          notes.get(selected).text=strInput;
        }
        break;
      case 2:
        println (" 0 : 2 " );
        notes.remove(selected); 
        break;
      case 3:
        println (" 0 : 3 " );
        menuOpen = false;
        menuOpenNumber = -1; 
        selected = -1; 
        break;
      default:
        break;
      } // inner switch
    }
    
    //
    void handleMenu1 (int command) {
      // local menu on empty space
      switch (command) {
    
      case 0:
        // add a new note 
        menuOpen=false; 
        strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text");
        if (strInput!=null) { 
          // note at mouse pos 
          DraggableB currNote = new DraggableB(mouseX, mouseY, 60, strInput);
          notes.add( currNote );
        } // if 
        break;
    
      case 1:
        println (" 1 : 1 " );
        menuOpen = false;
        menuOpenNumber = -1; 
        selected = -1; 
        break;
    
      case 2:
        println (" 1 : 2 " );
        break;
      case 3:
        println (" 1 : 3 " );
        break;
      case 4:
        println (" 1 : 4 " );
        break;
      default:
        break;
      } // switch
    }
    
    //
    void handleMenu2 (int command) {
      // main menu (=)
      switch (command) {
    
      case 0:
        // new sheet : delete all 
        menuOpen=false; 
        notes.clear();  // reset 
        selected = -1; 
        xPosForNewNote = 40; 
        yPosForNewNote = 70; 
        break;
    
      case 1:
        // new note
        newNote();  
        menuOpen=false;
        break;
    
      case 2:
        println (" 2 : 2 " );
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          currNote.posX = int(random ( 40, 60 ));
          currNote.posY = int(random ( 60, 80 ));
        } // for  
        menuOpen=false;
        break;
    
      case 3:
        int posX=50; 
        int posY=70; 
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          currNote.posX = posX;
          currNote.posY = posY;
          posX+=currNote.size+12;
          if (posX > width - 70) {
            posX=50;
            posY+=70;
          }
        } // for  
        menuOpen=false;
        break;
    
      case 4:
        //cancel
        menuOpen=false;
        break;
    
      default:
        break;
      } // inner switch
    }
    // ----------------------------------------------------------------
    // commands from the menus 
    
    void newNote() {
      String strInput = javax.swing.JOptionPane.showInputDialog("Please enter new text");
      if (strInput!=null) { 
        DraggableB currNote = new DraggableB(xPosForNewNote, yPosForNewNote, 60, strInput);
        notes.add( currNote );
        xPosForNewNote += 80; 
        if ( xPosForNewNote > width-40) {
          xPosForNewNote = 40;
          yPosForNewNote += 80;
        } // if
      } // if
    } // func 
    
    // ----------------------------------------------------------------
    // minor tools 
    
    void showStatusBar ( String myText ) {
      rectMode(CORNER);
      noStroke();
      fill(111);
      rect(0, height-30, width, 30);
      fill(255);
      text (myText, 20, height-8);
      rectMode(CENTER);
    }
    
    void menuSign(int x, int y) {
      rectMode(CORNER);
      noStroke();
      fill(100);
      int w = 20;
      int h = 4;
      rect( x, y, w, h, 8 );
      rect( x, y+h+2, w, h, 8 );
      rect( x, y+(h+2)*2, w, h, 8 );
      rectMode(CENTER);
    }
    
    // =======================================
    // classes 
    // 
    
    class DraggableB {
    
      int posX;
      int posY;
      int xOffset;
      int yOffset;
    
      int size;
      int halfSize;
    
      boolean over;
      boolean lock;
    
      String text=""; 
    
      DraggableB(int tempX, int tempY, 
      int tempSize, 
      String tempText) {
        posX = tempX;
        posY = tempY;
        size = tempSize;
        halfSize = size/2;
        text = tempText;
      } // constr
    
      void update() {
        // update
        fill(255, 255, 0); // yellow 
        if (!menuOpen) {
          if (mouseX >= posX - halfSize && mouseX <= posX + halfSize && 
            mouseY >= posY - halfSize && mouseY <= posY + halfSize) {
            over = true;  
            if (!lock) {
              stroke(0); 
              fill(255, 0, 0); // red
              //size = 30;
            } 
            else {
              fill(0, 0, 255); // blue
            } // else
          } // if  
          else {
            stroke(0);
            fill(255, 255, 0); // yellow 
            // size = size - 10; // ???
            over = false;
          } //else
        } // if
      } // method
    
      void display() {
        //noStroke();
        stroke(90);
        rect(posX, posY, size, size, 
        0, 0, 23, 0);
        fill(0); 
        if (text!=null)
          text(text, posX+2, posY+2, size, size);
      } // method
      //
    } // class 
    // 
    // =====================================
    
    class LocalMenu {
      //
      PVector pos = new PVector();
      PVector size = new PVector();
      String name;
      String textMouseOver;
      ArrayList<LineInTheLocalMenu> lines = new ArrayList();  
      //
      // constr
      LocalMenu ( 
      float x_, float y_, 
      String name_, 
      String textMouseOver_, 
      ArrayList<LineInTheLocalMenu> lines_ ) {
        pos.x=x_;
        pos.y=y_;
        name=name_;
        textMouseOver=textMouseOver_;
        lines=lines_;
        //
        // get the size of this menu
        float myWidth= textWidth (name) ;
        int i = 0; 
        for (LineInTheLocalMenu oneLine : lines ) {
          if (myWidth<textWidth (oneLine.content)) 
            myWidth = textWidth (oneLine.content) ;
          i++;
        }
        size.x = myWidth+15;
        size.y = 15*i+15+15+4; // to do
        //
      } // constr
      //
      void display() {
        // 1st: rect 
        rectMode(CORNER);
        noStroke();
        fill (121);
        rect(pos.x, pos.y, size.x, size.y );
        // 2nd: headline 
        fill(0);
        text(name, pos.x+15, pos.y+15);
        // 3rd: normal menu lines 
        int i=0;
        boolean doneStatusBar = false;
        for (LineInTheLocalMenu oneLine : lines ) {
          fill(245);
          text(oneLine.content, pos.x+15, pos.y +15*i+15+15+4); // to do 
          i++;
          oneLine.pos.x = pos.x+1;
          oneLine.pos.y = pos.y+15*i-29+15+15+4;    // to do
          if (oneLine.mouseOver() && mouseX < pos.x+size.x  && !doneStatusBar )
          {
            oneLine.over = true; 
            showStatusBar ( oneLine.textMouseOver );
            doneStatusBar=true;
          }
          else 
          {
            oneLine.over = false;
          }
        }
        if (!doneStatusBar) {
          if (mouseOver() ) {
            showStatusBar (  textMouseOver  );
            doneStatusBar=true;
          }
        }
        rectMode(CENTER);
      } // method
    
      boolean mouseOver() {
        if (mouseX>pos.x && 
          mouseY>pos.y && 
          mouseX<pos.x+size.x &&
          mouseY<pos.y+size.y ) {
          return true;
        }
        return false;
      } // method
    
      //
    
      int overWhichLine() {
        int i=0;
        for (LineInTheLocalMenu oneLine : lines ) {
          fill(245);
          // text(oneLine.content, pos.x+15, pos.y +15*i+15+15+4); // to do 
          i++;
          oneLine.pos.x = pos.x+1;
          oneLine.pos.y = pos.y+15*i-29+15+15+4;    // to do
          if (oneLine.mouseOver() && mouseX < pos.x+size.x )
          {
            oneLine.over = true; 
            return oneLine.commandID;
          }
        } // for
        return -1;
      } // func 
    
      //
    } // class
    
    // ===============================================================
    
    class LineInTheLocalMenu {
    
      PVector pos = new PVector();
      String content;
      String textMouseOver;
      boolean over = false; 
      int commandID=0;
      // 
      // constr
      LineInTheLocalMenu ( String content_, String textMouseOver_, int commandID_ ) {
        content=content_;
        textMouseOver=textMouseOver_;
        commandID = commandID_;
      }// constr
      //
      boolean mouseOver() {
        if (mouseX>pos.x && 
          mouseY>pos.y && 
          mouseY<pos.y + 19 ) {
          return true;
        }
        return false;
      } // method
      //
    } // class 
    
    // =============================================
    
  • Yeah, I guess, but...I don't know, let's see it then...

  • yeah, maybe it's not what you need or what you're planning, don't worry!

    ;-)

  • It's basically the same thing, just added that button for more options, now the size is working and a little minor changes to the design. I could use this version, but it's just more complicated than the other...so it's not like I'm not appreciating your work, but I think I will go with the previous version though.

  • edited June 2014

    yes, absolutely, do as you like.

    btw, now you can

    • stack all notes

    • spread all notes

    you got 3 different menus:

    • right click on a note,

    • right click on a empty space / canvas,

    • left click on menu symbol ( = )

    ;-)

  • Anyway, great work! But just for curiosity, it seems to you like it's an original idea? I mean, it's not like Google Keep or other Note Manager. It has this unique arrangement of notes. Other apps for notes just display notes as a list, but this one lets you see them like you would have them pinned on a wall, you know what I mean?

  • what are your plans?

    • I thought of save / load to hard drive

    • do a slide show of the notes (display each full screen for 5 secs and then go on or display one till mouse click)

    • having different types of notes (with head line, round ones...)

    • connecting them with lines

    • do the stacking and spreading with animation

    • when displaying one note full screen, there could be buttons on the note to jump to another note

    ;-)

  • you wrote

    But just for curiosity, it seems to you like it's an original idea? I mean, it's not like Google Keep or other Note Manager. It has this unique arrangement of notes. Other apps for notes just display notes as a list, but this one lets you see them like you would have them pinned on a wall, you know what I mean?

    could be, I am not sure...

    I don't know so many other note handlers...

  • I think outlook does its notes that way: in a kind of grid

  • you can change size of each note with + and - or via the local menu

    the length of the text could also change the size

  • Well I have a lot more ideas than you can imagine. That's what it is wrong. I have a lot of ideas, but can't implement them. So let's see: Wooden background so it would be like a real noteblock, with notes pined on it; Import and export from Excel files (there is a library for that); I also thought at saving and loading from hard drive; Folders for notes; Getting rid of the windows frame and add just a thin bar at the top with a rounded X button for closing the app; No menus, just buttons on the side (I have a very beautiful button with animation, you can see it at my Button Bug disscussion); Larger workspace (like 900x700); Different colors for notes; Every note should have a title that is displayed at the top and a content that is shown when pressing on it. Also thought about different size, but I don't think it is wise; Stack and spread- not very useful, don't you think?;

  • all very nice, great ideas!

    ;-)

  • edited June 2014

    I don't have the time to do it. And I can do it not for Android or something.

    but let's see

    • Wooden background so it would be like a real noteblock, with notes pined on it;

    ok, easy just find a free image on the net and use with command background()

    • Import and export from Excel files (there is a library for that);

    what should be stored in the file? position, title, text? Color?

    • I also thought at saving and loading from hard drive;

    ok, easy

    • Folders for notes;

    not understood, you mean folders on the hard drive?

    • Getting rid of the windows frame and add just a thin bar at the top with a rounded X button for closing the app;

    easy, take

    void setup() {
    
      size(displayWidth, displayHeight);
    

    and start with ctrl-shift-r

    • No menus, just buttons on the side (I have a very beautiful button with animation, you can see it at my Button Bug disscussion);

    easy, but how can you tell what button does what? Is there a text under each button?

    • Larger workspace (like 900x700);

    see above: size(displayWidth, displayHeight);

    • Different colors for notes;

    ok, easy, but randomly color? Or is there a color selector when creating a new note?

    • Every note should have a title that is displayed at the top and a content that is shown when pressing on it.

    ok, easy

    • Also thought about different size, but I don't think it is wise;

    ok

    • Stack and spread- not very useful, don't you think?;

    Right.

    ;-)

  • Thank you, but they value nothing if not implemented. So the windows frame and stuff is already made. I have the buttons and the animations for them, I already have the background and the background wooden image, I think I know how to change the colors of the notes. The rest I have no idea how to make... But don't worry I think it is enough to keep me busy for the next week. Thank you again for all the help!

  • What you did there with the code is very hard to understand, but I can get most of it vaguely. I know how to write and read from a file but the save and load function is beyond my power, also the exceel import/export. And yes, there will be some text under the buttons, and I won't use displayWidth and displayHeight for the size parameters. There is another way, just search on google and you will find it.

  • save and load function:

    I just take the text of the note, the position x and y and the size etc. and join it with "#" in one line, for-loop and save it with saveStrings()

    then retrieve it with loadStrings and use split() and then build up our ArrayList with a for-loop

    not sure though if this works in Android mode or so...

    ;-)

  • FluFlu
    edited June 2014

    The app is for windows, not for android. I already made a program that reads from a .txt file and I know how to use split and join and other stuff, but it is all a mess in my head, and I simply don't trust myself that I can do this.

  • FluFlu
    edited June 2014

    So...

  • shall I give it a go?

  • No, thank you, I'm good. But I might have some questions about that color notes thing I am gonna do maybe the day after tomorrow. Until then...

  • But maybe I could also do that on my own, who knows?

  • too late, it's done already

    this is load and save (part of the menu structure of my sketch)

    case 2:
        // load (we're appending the notes here)
        String lines[] = loadStrings("nouns.txt");
        println("there are " + lines.length + " lines");
        // for each line 
        for (int i = 0 ; i < lines.length; i++) {
          println(lines[i]);
          DraggableB newNote;
          // split the line 
          String [] lineContent = split(lines[i], '#'); 
          // fill the content into the new note 
          newNote = new DraggableB ( 
          int (lineContent  [ 1 ]), 
          int (lineContent  [ 2 ]), 
          int (lineContent  [ 3 ]), 
          lineContent [0]  ) ;
          notes.add(newNote);
        } // for 
        break;
    
      case 3:
        // save 
        String[] list = new String [notes.size()];
        for (int i = 0; i < notes.size(); i++) {
          DraggableB currNote = notes.get(i);
          list[i] = 
            currNote.text + "#" +  
            currNote.posX + "#" +
            currNote.posY + "#" +
            currNote.size + "#" ;
        }
        // Writes the strings to a file, each on a separate line
        saveStrings("nouns.txt", list);
        break;
    
  • I can't literally understand the code, and why should I use it with selectInput? I don't have more .txt files, it's just one.

  • when you only have one file, you are fine without selectInput()

  • The only problem I don't know to solve and I really need to do this is to modify the input window. You know, where you put the text for the note. There I need to have a multiline input box, not just a single long line, that's annoying. I was also wondering if we could get rid of the window, and make that text box right next to the note we make or modify.

  • edited June 2014

    the idea of multiple txt files was that you make different sheets with notes save them each in a different txt file and can load each of them when you need them...

    • one txt file contains all notes of one sheet

    • different txt files contain different sets of notes

    • like in word: different files for different texts

  • I already thinked of different sheets, but it's getting to far away from my knowledge. Maybe for you it is simple, but for me all this code is just mind-blowing.

  • edited June 2014

    this is a text box as you mentioned

    /* built with Studio Sketchpad:
     *   http://      sketchpad.cc
     *
     * observe the evolution of this sketch:
     *   http://      studio.sketchpad.cc/sp/pad/view/ro.s0xWbQ7L2BL/rev.31
     *
     * authors:
     *   GoToLoop
    
     * license (unless otherwise specified):
     *   creative commons attribution-share alike 3.0 license.
     *   http://    creativecommons.org/licenses/by-sa/3.0/
     */
    
    
    
    /**
     * TextBox Writer (v2.4)
     * by  Inarts (2013/Oct)
     * mod GoToLoop
     *
     * forum.processing.org/two/discussion/423/
     * how-to-write-a-class-for-text-area-without-using-any-library
     *
     * studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest
     */
    
    TextBox tbox;
    
    void setup() {
      size(640, 480);
      //frameRate(20);
      smooth(4);
    
      rectMode(CORNER);
      textAlign(LEFT);
      strokeWeight(1.5);
    
      instantiateBoxes();
      tbox.isFocused = true;
    }
    
    void draw() {
      background(#778C85);
      tbox.display();
    }
    
    void keyTyped() {
      final char k = key;
      if (k == CODED)  return;
    
      final int len = tbox.txt.length();
    
      if (k == BACKSPACE)  tbox.txt = tbox.txt.substring(0, max(0, len-1));
      else if (len >= tbox.lim)  return;
      else if (k == ENTER | k == RETURN)     tbox.txt += "\n";
      else if (k == TAB & len < tbox.lim-3)  tbox.txt += "    ";
      else if (k == DELETE)  tbox.txt = "";
      else if (k >= ' ')     tbox.txt += str(k);
    }
    
    void keyPressed() {
      if (key != CODED)  return;
      final int k = keyCode;
    
      final int len = tbox.txt.length();
    
      if (k == LEFT)  tbox.txt = tbox.txt.substring(0, max(0, len-1));
      else if (k == RIGHT & len < tbox.lim-3)  tbox.txt += "    ";
    }
    
    void instantiateBoxes() {
      tbox = new TextBox(
      width>>2, height/4 + height/16, // x, y
      width - width/2, height/2 - height/4 - height/8, // w, h
      215, // lim
      0300 << 030, color(-1, 040), // textC, baseC
      color(-1, 0100), color(#FF00FF, 0200)); // bordC, slctC
    }
    
    // =======================================
    
    class TextBox { // demands rectMode(CORNER)
      final color textC, baseC, bordC, slctC;
      final short x, y, w, h, xw, yh, lim;
    
      boolean isFocused;
      String txt = "";
    
      TextBox(int xx, int yy, int ww, int hh, int li, 
      color te, color ba, color bo, color se) {
        x = (short) xx;
        y = (short) yy;
        w = (short) ww;
        h = (short) hh;
    
        lim = (short) li;
    
        xw = (short) (xx + ww);
        yh = (short) (yy + hh);
    
        textC = te;
        baseC = ba;
        bordC = bo;
        slctC = se;
      }
    
      void display() {
        // demands rectMode(CORNER)
        rectMode(CORNER);
        stroke(isFocused? slctC : bordC);
        fill(baseC);
        rect(x, y, w, h);
    
        fill(textC);
        text(txt + blinkChar(), x, y, w, h);
      }
    
      String blinkChar() {
        return isFocused && (frameCount>>2 & 1) == 0 ? "_" : "";
      }
    
      boolean checkFocus() {
        return isFocused = 
          mouseX > x & mouseX < xw & 
          mouseY > y & mouseY < yh;
      }
    } // class 
    //
    
  • Wow, you did these just now?

  • Oh, it's made by someone else, I see.

  • no, it's by gotoloop, I just looked it up

    it's a little hard too read...

    ;-)

    I'm off...

    please ask again if you need something

Sign In or Register to comment.