在Processing中循环遍历变量数组

时间:2016-05-23 00:39:01

标签: arrays processing typography

我正在尝试创建一个按下键盘字符的程序,然后以6种不同的字体显示(在六个不同颜色的方块内)。我一次只显示六种字体,但我希望能够在用鼠标左键单击时切换字体。截至目前,右键单击将鼠标指针下方的字体转换为斜体。

我的问题是:我如何制作一组PFont变量,所以当我左键点击一个"字符&#34 ;;我可以通过一些字体循环,即(Helvetica,Futura,Optima)。

我的代码如下:



boolean button1 = false;
boolean button2 = false;
boolean button3 = false;
boolean button4 = false;
boolean button5 = false;
boolean button6 = false;

int x1 =0;
int y1 =0;
int w1 = 400;
int h1 = 600;

int x2 = 400;
int y2 = 0;
int w2 = 400;
int h2 = 600;

int x3 = 800;
int y3 = 0;
int w3 = 400;
int h3 = 600;

int x4 = 0;
int y4 = 600;
int w4 = 400;
int h4 = 600;

int x5 = 400;
int y5 = 600;
int w5 = 400;
int h5 = 600;

int x6 = 800;
int y6 = 600;
int w6 = 400;
int h6 = 600;

String hel_reg = "Helvetica.vlw";
String din_reg = "DIN.vlw";
String did_reg = "Didot.vlw";
String roc_reg = "Rockwell.vlw";
String bel_reg = "Bell.vlw";
String sab_reg = "Sabon.vlw";

String hel_itl = "Helvetica_itl.vlw";
String din_itl = "DIN_itl.vlw";
String did_itl = "Didot_itl.vlw";
String roc_itl = "Rockwell_itl.vlw";
String bel_itl = "Bell_itl.vlw";
String sab_itl = "Sabon_itl.vlw";

String fut_reg = "Futura.vlw";
String opt_reg = "Optima.vlw";
String cla_reg = "Clarendon.vlw";
String uni_reg = "Unit_Slab_Pro.vlw";
String mrs_reg = "Mrs_Eaves.vlw";
String cas_reg = "Caslon.vlw";

String fut_itl = "Futura_Italics.vlw";

String name = "FranklinGothic-Demi-30.vlw";

String letters = "";

PFont Helvetica;
PFont DIN;
PFont Didot;
PFont Rockwell;
PFont Bell;
PFont Sabon;

PFont Helvetica_Italics;
PFont DIN_Italics;
PFont Didot_Italics;
PFont Rockwell_Italics;
PFont Bell_Italics;
PFont Sabon_Italics;

PFont Futura;
PFont Optima;
PFont Clarendon;
PFont Unit_Slab;
PFont Mrs_Eaves;
PFont Caslon;

PFont Futura_Italics;

PFont Franklin_Gothic;


void setup() {
  size(1200, 1300);
  Helvetica = loadFont(hel_reg);
  DIN = loadFont(din_reg);
  Didot = loadFont(did_reg);
  Rockwell = loadFont(roc_reg);
  Bell = loadFont(bel_reg);
  Sabon = loadFont(sab_reg);

  Helvetica_Italics = loadFont(hel_itl);
  DIN_Italics = loadFont(din_itl);
  Didot_Italics = loadFont(did_itl);
  Rockwell_Italics = loadFont(roc_itl);
  Bell_Italics = loadFont(bel_itl);
  Sabon_Italics = loadFont(sab_itl);

  Futura = loadFont(fut_reg);
  Optima = loadFont(opt_reg);
  Clarendon = loadFont(cla_reg);
  Unit_Slab = loadFont(uni_reg);
  Mrs_Eaves = loadFont(mrs_reg);
  Caslon = loadFont(cas_reg);

  Futura_Italics = loadFont(fut_itl);

  Franklin_Gothic = loadFont(name);
}

void draw() {
  noStroke();
  fill(176, 116, 143);
  rect(x1, y1, w1, h1);

  fill(135, 184, 194);
  rect(x2, y2, w2, h2);

  fill(225, 205, 109);
  rect(x3, y3, w3, h3);

  fill(230, 111, 83);
  rect(x4, y4, w4, h4);

  fill(64, 78, 87);
  rect(x5, y5, w5, h5);

  fill(245, 242, 238);
  rect(x6, y6, w6, h6 );

  fill(117, 172, 151);
  rect(0, 1200, 1200, 100 );

  //stroke(245, 242, 238);
  //strokeWeight(2);

  //line(400, 1200, 400, 1300);
  //line(800, 1200, 800, 1300);



  if (button1) {
    fill(225, 205, 109);
    textFont(Helvetica_Italics);
    textAlign(CENTER);
    text(key, 200, 400 );
  } else {
    fill(225, 205, 109);
    textFont(Helvetica);
    textAlign(CENTER);
    text(key, 200, 400 );
  }

  if (button2) {
    fill(64, 78, 87);
    textFont(Didot_Italics);
    textAlign(CENTER);
    text(key, 600, 400 );
  } else {
    fill(64, 78, 87);
    textFont(Didot);
    textAlign(CENTER);
    text(key, 600, 400 );
  }

  if (button3) {
    fill(176, 116, 143);
    textFont(Bell_Italics);
    textAlign(CENTER);
    text(key, 1000, 400);
  } else {
    fill(176, 116, 143);
    textFont(Bell);
    textAlign(CENTER);
    text(key, 1000, 400);
  }

  if (button4) {
    fill(245, 242, 238);
    textFont(DIN_Italics);
    textAlign(CENTER);
    text(key, 200, 1000);
  } else {
    fill(245, 242, 238);
    textFont(DIN);
    textAlign(CENTER);
    text(key, 200, 1000);
  }

  if (button5) {
    fill(230, 111, 83);
    textFont(Rockwell_Italics);
    textAlign(CENTER);
    text(key, 600, 1000 );
  } else {
    fill(230, 111, 83);
    textFont(Rockwell);
    textAlign(CENTER);
    text(key, 600, 1000 );
  }

  if (button6) {
    fill(135, 184, 194);
    textFont(Sabon_Italics);
    textAlign(CENTER);
    text(key, 1000, 1000);
  } else {
    fill(135, 184, 194);
    textFont(Sabon);
    textAlign(CENTER);
    text(key, 1000, 1000);
  }

  textAlign(CENTER);
  textFont(Franklin_Gothic);
  fill(245, 242, 238);

  text("HELVETICA", 200, 1241.58);
  text("DIN", 200, 1277.5);
  text("DIDOT", 600, 1241.58);
  text("ROCKWELL", 600, 1277.5);
  text("BELL", 1000, 1241.58);
  text("SABON", 1000, 1277.5);
}

void mousePressed() {
  if (mouseX > x1 && mouseX < x1+w1 && mouseY > y1 && mouseY < y1+h1 && mouseButton == RIGHT) {
    button1 = !button1;
  }
  if (mouseX > x2 && mouseX < x2+w2 && mouseY > y2 && mouseY < y2+h2 && mouseButton == RIGHT) {
    button2 = !button2;
  }
  if (mouseX > x3 && mouseX < x3+w3 && mouseY > y3 && mouseY < y3+h3 && mouseButton == RIGHT) {
    button3 = !button3;
  }
  if (mouseX > x4 && mouseX < x4+w4 && mouseY > y4 && mouseY < y4+h4 && mouseButton == RIGHT) {
    button4 = !button4;
  }
  if (mouseX > x5 && mouseX < x5+w5 && mouseY > y5 && mouseY < y5+h5 && mouseButton == RIGHT) {
    button5 = !button5;
  }
  if (mouseX > x6 && mouseX < x6+w6 && mouseY > y6 && mouseY < y6+h6 && mouseButton == RIGHT) {
    button6 = !button6;
  }


  if (mouseButton == LEFT) {
  }
}
&#13;
&#13;
&#13;

您可能无法运行代码,因为我在数据文件夹上有vlw文件。我不知道如何将其包含在这里。

1 个答案:

答案 0 :(得分:0)

第1步:创建一个数组(或ArrayList)并将字体添加到其中。

第2步:创建一个变量,跟踪数组的哪个索引包含当前字体。

第3步:每当您想要更改正在使用的字体时,都会增加该变量。