0

我正在编写一个简单的图表来解析一个 .tsv 文件,该文件有 6 列和 9 行。我试图在我的图表上放置一些文本,这些文本根据第三列中存在的数据进行着色。我能够获得屏幕上的所有颜色,但由于某种原因,以红色结尾的文本是与我想要其文本为红色的行下方的行(在 tsv 文件中)对应的文本。例如,虽然我希望自由党候选人填写 (200,60,60),但出现在下面一行中的 Parti Quebecois 候选人却变成了红色。接下来,新民主党候选人最终获得(155,191,219)的填补。代码如下所示:

PImage mapOfCanada; // background map
Premier[] premiers; // premiers data

void setup() {
  size(800, 800);
  // modified mapOfCanada from http://www.theblog.ca/map-canada
  mapOfCanada = loadImage("bigmapofcanada.png");
  // from http://en.wikipedia.org/wiki/List_of_current_Canadian_first_ministers
  Table table = new Table("premiers.tsv");
  int rows = table.getRowCount();
  premiers = new Premier[rows];
  // read through each row of the source data to populate our premiers array
  for (int i=0; i<rows; i++) {
    String name = table.getString(i, 0);
    String province = table.getString(i, 1);
    String party = table.getString(i, 2);
    String imgFile = table.getString(i, 3);
    PImage img = loadImage(imgFile);
    float x = table.getFloat(i,4);
    float y = table.getFloat(i,5);
    premiers[i] = new Premier(name, province, party, img, x, y);
  }
}

void draw() {
  background(255);
  // draw the background image with a light tint
  tint(255, 25);
  image(mapOfCanada, 0, 0);

  // draw each premier
  noTint();
  for (Premier premier : premiers) {
    image(premier.img, premier.x, premier.y);
  }
  //drawing lines for those premier images that cannot fit in the alloted province space
  line(158,560,145,460); //Alberta
  line(300,560,340,500); //Manitoba
  line(650,365,670,410); //Newfoundland
  line(750,385,710,535); //PEI
  line(730,575,720,550); //Nova Scotia
  line(670,595,680,560); //New Brunswick

  //adding text labels
  for (Premier premier : premiers) { //reading through the source data in a loop
   textSize(10); //making the text size small yet readable
   textAlign(CENTER); //making sure the text is centered above the image
    text(premier.name, premier.x+50, premier.y-10); //positioning the text in relation to the x and y coordinates on the source data

{
String string1 = new String("Liberal");
String string2 = new String("Parti Quebecois");
String string3 = new String("New Democratic");
String string4 = new String ("Progressive Conservative");
String string5 = new String ("Saskatchewan Party");
String string6 = new String ("Yukon Party");

if (premier.party.equals("Liberal")) {
  fill(200,60,60);  
} 
else if (premier.party.equals("Parti Quebecois")) {
  fill(155,191,219);
}
else if (premier.party.equals("New Democratic")) {
  fill(180,151,107);
}
else if(premier.party.equals("Progressive Conservative")) {
  fill(96,104,250);
}
else if(premier.party.equals("Saskatchewan Party")) {
  fill (107,180,119);
}
else if(premier.party.equals("Yukon Party")) {
  fill (47,85,232);
}
  else {
  fill (0,0,0);
}
}
  }
}

class Premier {
  String name, province, party;
  PImage img; // this is the thumbnail image
  float x, y; // these are the coordinates for the thumbnail
  Premier(String name, String province, String party, PImage img, float x, float y) {
    this.name = name;
    this.province = province;
    this.party = party;
    this.img = img;
    this.x = x;
    this.y = y;
  }
}

任何帮助回复:我做错了什么将不胜感激!我已经编辑了帖子以包含完整的代码。

谢谢!

4

2 回答 2

0

根据您遇到的问题的描述,它可能与索引错误有关 - 即使用基于 0 的索引,而实际上它是基于 1 的索引,反之亦然。但是如果没有循环代码和 fill() 代码,就很难查明问题所在。

于 2013-02-26T11:38:38.730 回答
0

似乎是包装您发布的代码的循环中的一次性错误。

于 2013-02-26T05:47:22.370 回答