代码之家  ›  专栏  ›  技术社区  ›  NetTemple

要在循环中用作Pshape的进程字符串名称

  •  0
  • NetTemple  · 技术社区  · 6 年前

    我试图拉一个sting变量并将其放置在形状函数中,以便显示正确的状态。我有一个单一的(硬编码)版本,但我正试图通过添加循环来提高代码的效率和动态性。

    请参见以下代码:

    PShape usa;
    
    PShape Alabama;
    PShape Alaska;
    PShape Arizona;
    PShape Arkansas;
    
    void setup() {
      size(1000, 1000);  
      usa = loadShape("map.svg");
    
      Alabama= usa.getChild("AL");
      Alaska= usa.getChild("AK");
      Arizona= usa.getChild("AZ");
      Arkansas= usa.getChild("AR");
    }
    
    void draw() {
      background(255);
      Table table = loadTable("data.csv", "header");
      for (int i = 0; i < table.getRowCount(); i++) {
    
        // Gets the 1st (then 2nd, then 3rd...) TABLE ROW
        TableRow row = table.getRow(i);
    
        //  Gets the data values and places in variables
        String state = row.getString("state");
        String state_abv = row.getString("state_abv");
        float percentage = row.getFloat("percentage");
    
    
        // Draw the full map
        shape(usa, 0, 0);
    
        //*****************************
        // THIS IS THE DYNAMIC ATTEMPT
        state.disableStyle();
        fill(0);
        shape(state, 0, 0);
        text(state_abv, 100 100);
        text(percentage, 100 150);
    
    
        // THIS WORKS BUT IS A SINGLE STATE AND HARD CODE...
        // NewJersey.disableStyle();
        // fill(R, 255, 255);
        // shape(NewJersey, 0, 0);
    
        // DATA FILE IS LIKE SO:
        // state, state_abv,percentage
        /*
         Alabama,AL,21.5
         Alaska,AK,19
         Arizona,AZ,14.7
         Arkansas,AR,23.6
         */
      }
    }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Kevin Workman    6 年前

    如果我理解你的问题,听起来你在寻找 HashMap 班类似于数组映射索引的方式(0、1、2、3…)到某个值 哈希图 映射任何对象(包括 String 喜欢 "PA" , "VA" , "CA" ...) 到某个值。下面是一个基本示例:

    HashMap<String, String> map = new HashMap<String, String>();
    map.put("PA", "Harrisburg");
    map.put("VA", "Richmond");
    map.put("CA", "Sacramento");
    String city = map.get("VA");
    println(city);
    

    此示例映射 一串 关键点 一串 值,但可以将任何对象用作键或值。有关更多信息,请访问 the reference .

    如果您仍然无法弄清楚,请发布 MCVE 在一个新问题中,我们将从那里开始。祝你好运

        2
  •  0
  •   Cindy Meister Prabhuprasad NG    6 年前

    这是一个可行的解决方案,尽管填充颜色问题仍未解决。。。(这是一个单独的问题)。但这确实会将外部数据文件中的所有数据放入一个功能数组“states[]”

    PShape usa;
    PShape[] states;       // array to hold all states
    int numstates;         //count number of states
    
    void setup() {
      size(1000, 1000);
    
      //load the map
      usa = loadShape("map.svg");
      parseMap();          
      smooth();
      noLoop();
    }
    
    void draw() {
      background(200);
      drawMap();
    }
    
    // ***** ------------------------
    // ***** ----- PARSE MAP
    // ***** ------------------------
    void parseMap() {
      numstates = usa.getChildCount(); //count number of child shapes on map
      states = new PShape[numstates];
      //println("NUMSTATES " + numstates);
    
      // stateCodes = new String[numstates];
      usa.disableStyle(); //turn off styles for whole map
    
      //make state objects
      for (int i = 0; i < numstates; i++) {
        states[i] = usa.getChild(i);
        println("NUMSTATES " + states[i]);
      }
    }
    
    
    // ***** ------------------------
    // ***** ----- DRAW MAP
    // ***** ------------------------
    void drawMap() {
      for (int n = 0; n < numstates; n++) {
    
        Table table = loadTable("data.csv", "header");
        for (int i = 0; i < table.getRowCount(); i++) {
    
          // Gets the 1st (then 2nd, then 3rd...) TABLE ROW
          TableRow row = table.getRow(i);
    
          //  Gets the data values and places in variables
          String s = row.getString("state");
          String a = row.getString("abv");
          float p = row.getFloat("percent");
          int colora = row.getInt("colora");
          int colorb = row.getInt("colorb");
          int colorc = row.getInt("colorc");
          int loca = row.getInt("loca");
          int locb = row.getInt("locb");
    
          states[n].disableStyle();
          // fill(colora,colorb,colorc);          // PROBLEM HERE: It fills ALL states same color
          states[n].fill(colora,colorb,colorc);   // I AM TRYING TO FILL EACH STATE A DIFFERNET COLOR
          stroke(255,0,0);
          strokeWeight(2);
          shape(states[n],0,0);
          fill(255);
          text(p, loca, locb);
        }
        println("NUMSTATES " + numstates);
      }
    }