associate text and picture

edited July 2014 in How To...

Hello, I have generative text, but would like associate some words with some pictures.

exemple : If I have : String[] alea = {"cat","dog"};

and I would like associate "cat" with PImage cat;

How to do ?

I'm lost.

Thank you fo all.

Answers

  • edited July 2014 Answer ✓

    http://processing.org/reference/HashMap.html
    http://docs.oracle.com/javase/8/docs/api/java/util/Map.html

    final HashMap<String, PImage> imgWords = new HashMap();
    
    imgWords.put("cat", loadImage("cat.jpg"));
    imgWords.put("dog", loadImage("dog.jpg"));
    
  • edited July 2014 Answer ✓
    // forum.processing.org/two/discussion/6104/
    // associate-text-and-picture  
    
    static final String EXT = ".jpg", words[] = {
      "cat", "dog"
    };
    
    final HashMap<String, PImage> imgWords = new HashMap();
    
    void setup() {
      size(800, 600, JAVA2D);
      smooth(4);
    
      for (String w: words)
        imgWords.put(w, loadImage(w + EXT));
    }
    
  • Thank you fo all. I try.

  • I have this error : "The file "blabla.jpg" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable."

    I have the file in the folder data. So it's good no ? Or I forgot something ? I search.

    Thank you fo all.

  • Rather than just describe it, you should also provide the relevant code snippet of your problem!

  • edited July 2014

    Ok. I'm sorry. In fist time, I have no problem with my code. In second time, I have problem whith my code when I interact.

    Snippet :

    // forum.processing.org/two/discussion/6104/
    // associate-text-and-picture
    
    void mousePressed() {
    
      if (mouseX >= xText1 && mouseX <= xText1+textWidth(Mots1[randomText1]) && mouseY <= y && mouseY >= y-textFontSize) //Condition 
      {
    
        turn++;
    
        if (turn == 0) {
          isSelected1 = true;
        }
        else if (turn == 1) {
          isSelected2 = true;
        }
        else {
          isSelected3 = true;
        }
     for (String m : Mots1){
    if (isSelected1 && !isLocked1) {
      poem1 = finalMots1+Mots1[randomText1].toLowerCase(); //droite
      xPoem1Cible = largeurRandom+margeTexte; //position
      imgWords.put(a, loadImage(m + EXT));}
      isLocked1 = true; //boolean
        }
    

    randomText1 = round(random(0, ancre1.length-1)); }

  • edited July 2014

    It's hard to figure out your snippet b/c there are many variables which were declared & initialized somewhere else!
    What I can tell you is that we should avoid loading resources outside setup()!
    Use loadImage() and put() its reference in the HashMap structure within setup()!

  • In addition to using a Map, you could also create an Object that contains a String and PImage. Depends on exactly what you need to do with the data.

  • Finally, I haven't use "HashMap" because I had problem with data folder. I have just read :

    String EXT : ".png";
        if (isSelected1 && !isLocked1) { 
          poem1 = finalMots+Mots1[randomText1].toLowerCase();
          img1 = loadImage(Mots1[randomText1]+EXT);
          isLocked1 = true;
    }
    

    Thank you fo all.

Sign In or Register to comment.