我正在尝试拉出一个sting变量并将其放在一个shape函数中,以便显示正确的状态。我有一个(硬编码)版本工作,但我试图通过添加循环使代码高效和更动态。
请参阅以下代码:
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
*/
}
}
答案 0 :(得分:0)
如果我理解你的问题,听起来你正在寻找HashMap
课程。与数组如何将索引(0,1,2,3 ......)映射到值类似,HashMap
映射任何对象(包括String
,如"PA"
,{{ 1}},"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);
键映射到String
值,但您可以将任何对象用作键或值。更多信息可以在the reference找到。
如果您仍然无法解决问题,请在新问题中发布MCVE,我们将从那里开始。祝你好运。
答案 1 :(得分:0)
这是一个有效的解决方案,虽然FILL COLOR问题仍未解决......(这是一个单独的问题)。但这确实将来自外部数据文件的所有数据放入一个正常运行的数组“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);
}
}