0

我是一个处理初学者。理想情况下,我想在显示时随机更改每个单词的字体。但是,由于我对 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

4 回答 4

0

看看 jcaptcha RandomFontGenerator

于 2013-09-05T20:32:06.007 回答
0
if(i == nums){

您将整数值(数字)与整数数组(大量数字)进行比较。这是完全不可能的。

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

if(i==nums[i])

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

List<Integer> list = Arrays.asList(nums);
if(list.contains(i))
于 2013-09-05T20:47:28.300 回答
0

谢谢大家,尤其是constantlerner。我刚刚通过 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));
}

}
于 2013-09-06T20:16:06.613 回答
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();
}
于 2013-09-07T06:47:08.080 回答