处理:If语句中的数组

时间:2013-09-05 20:26:43

标签: java arrays if-statement fonts processing

我是一名处理初学者。理想情况下,我希望在每个单词显示时随机更改字体。但是,由于我对JAVA缺乏了解,我现在只想根据数组“nums”按特定数字更改每种字体。你能建议我用每个词改变字体的好主意吗?

PFont f1, f2, f3;
String message = "transText 2013";
int[] nums = {1, 3, 5, 7, 9, 11, 13};
float r,g,b = 0;
float rr = 0.5;
float gg = 0.1;
float bb = 0.3;
float n;


void setup(){
  size(800,400);
  frameRate(20);
  nums = new int[6];
  f1 = createFont("Arial-Black", 16, true);
  f2 = createFont("Courier", 16, true);
}

void draw(){

rr = rr + 0.02;
gg = gg + 0.02;
bb = bb + 0.02;

background(35);

r = map(random(rr),0,1,100,255);
g = map(random(gg),0,1,100,255);
b = map(random(bb),0,1,100,255);
color n = color(r,g,b);
fill(n);
// The first character is at pixel 10.
int x = 50; 
for(int i = 0; i < message.length(); i++) {
  if(i == nums){
  textFont(f1);
  }else{
  textFont(f2);
  };

  textSize(random(12,120));
  // Each character is displayed one at a time with the charAt() function.
  text(message.charAt(i),x,height/2);
  // All characters are spaced 50 pixels apart.
  x += textWidth(message.charAt(i)); 
}
}

4 个答案:

答案 0 :(得分:0)

查看jcaptcha RandomFontGenerator

答案 1 :(得分:0)

if(i == nums){

将整数值(数字)与整数数组(很多数字)进行比较。 这完全不可能。

您可以将整数值与数组的ELEMENT进行比较。 例如:

if(i==nums[i])

或者您可以看到数组是否包含值。

List<Integer> list = Arrays.asList(nums);
if(list.contains(i))

答案 2 :(得分:0)

感谢所有人,尤其是常客。我刚用RandomFontGenerator方法解决了这个问题。以下是最终代码。我将继续研究这个程序,以不同的方式更改每个单词字体。再次感谢你。

PFont f1, f2, f3, fontR;
String message = "transText 2013";
float r,g,b = 0;
float rr = 0.5;
float gg = 0.1;
float bb = 0.3;
float n;
int randomFont;

void setup(){
  size(800,200);
  frameRate(20);
  f1 = createFont("Arial-Black", 16, true);
  f2 = createFont("Courier", 16, true);
  f3 = createFont("FuturaStd-Light", 16, true);

}

void draw(){

rr = rr + 0.02;
gg = gg + 0.02;
bb = bb + 0.02;

background(35);

  float rF = random(1,10); // Picking random number 1 to 10(depending on how much range you want)
  randomFont = int(rF); // rF into int

  // Separating range by if-statement in 3 parts
  if(rF < 3){
  fontR = f1;}
  else if(rF > 3 && rF < 6){
  fontR = f2;}
  else{
  fontR = f3;}

// fontR into textFont
textFont(fontR);


r = map(random(rr),0,1,100,255);
g = map(random(gg),0,1,100,255);
b = map(random(bb),0,1,100,255);
color n = color(r,g,b);
fill(n);
// The first character is at pixel 50.
int x = 30;

for(int i = 0; i < message.length(); i++) { 
  textSize(random(12,120));
  // Each character is displayed one at a time with the charAt() function.
  text(message.charAt(i),x,170);
  // All characters are spaced 10 pixels apart.
  x += textWidth(message.charAt(i));
}

}

答案 3 :(得分:0)

您可以使用RiTa库:

import rita.*;

RiText[] rts;
String[] fonts = { "arial","times","courier" };

void setup() {

   rts = RiText.createLetters(this,"transText 2013",5,30,100);
}

void draw() {

  background(255);
  for (int i = 0; i < rts.length; i++) {   
    rts[i].fill(RiText.randomColor());
    rts[i].font(fonts[(int)random(fonts.length)], 14);
  }
  RiText.drawAll();
}