如何区分字符串中单个字母的文本颜色?
How to differentiate text colour on individual letters in a string?
我是这个编程领域的新手,正在研究视觉节拍器。每个字母都分配了一个鼓模式。
稍后我将努力为它添加点击声音,但我想先弄清楚视觉效果。
除非你们中的一些人能帮我在每次字母变化时添加声音,否则那就太好了!
不过关于视觉部分,我想区分各个字母的文本颜色,这样左边的会更突出,右边的会更暗。
你们中的一些人能帮我吗?
最好的,SHB
String[] words = {"A ", "B ", "C ", "D ", "E ", "F "};
int newIndex = 0;
int oldIndex = -1;
PFont SansSerif;
void setup() {
size(700, 500);
background(0);
SansSerif = createFont("SansSerif", 162);
textFont(SansSerif);
}
void draw() {
frameRate(.6);
background(0);
// Get a random element from array
newIndex = int(random(words.length));
if (oldIndex > -1) {
fill(150);
text(words[oldIndex] + words[newIndex], 150, 300);
println("old =", words[oldIndex] + " : " + "new =", words[newIndex] );
} else {
fill(150);
text(words[newIndex], 150, 300);
println("new =", words[newIndex]);
}
oldIndex = newIndex;
}
通过将您选择的字母更改为字符串,然后遍历字符,您可以使用 charAt() 将每个字符更改为不同的颜色。
String[] words = {"A ", "B ", "C ", "D ", "E ", "F "};
int newIndex = 0;
int oldIndex = -1;
PFont SansSerif;
String beat = "";
int x = 0;
void setup() {
size(700, 500);
background(255); // Changed to lighter background
SansSerif = createFont("SansSerif", 162);
textFont(SansSerif);
}
void draw() {
frameRate(.6);
background(255); // Changed to lighter background
// Get a random element from array
newIndex = int(random(words.length));
if (oldIndex > -1) {
beat = words[oldIndex] + words[newIndex];
int x = 150;
for (int i = 0; i < beat.length(); i++){
if(i == 0){
fill(0);
} else {
fill(150);
}
text(beat.charAt(i), x, 300);
x += 60;
}
println("old =", words[oldIndex] + " : " + "new =", words[newIndex] );
} else {
fill(0);
text(words[newIndex], 150, 300);
println("new =", words[newIndex]);
}
oldIndex = newIndex;
}
我是这个编程领域的新手,正在研究视觉节拍器。每个字母都分配了一个鼓模式。
稍后我将努力为它添加点击声音,但我想先弄清楚视觉效果。 除非你们中的一些人能帮我在每次字母变化时添加声音,否则那就太好了!
不过关于视觉部分,我想区分各个字母的文本颜色,这样左边的会更突出,右边的会更暗。
你们中的一些人能帮我吗?
最好的,SHB
String[] words = {"A ", "B ", "C ", "D ", "E ", "F "};
int newIndex = 0;
int oldIndex = -1;
PFont SansSerif;
void setup() {
size(700, 500);
background(0);
SansSerif = createFont("SansSerif", 162);
textFont(SansSerif);
}
void draw() {
frameRate(.6);
background(0);
// Get a random element from array
newIndex = int(random(words.length));
if (oldIndex > -1) {
fill(150);
text(words[oldIndex] + words[newIndex], 150, 300);
println("old =", words[oldIndex] + " : " + "new =", words[newIndex] );
} else {
fill(150);
text(words[newIndex], 150, 300);
println("new =", words[newIndex]);
}
oldIndex = newIndex;
}
通过将您选择的字母更改为字符串,然后遍历字符,您可以使用 charAt() 将每个字符更改为不同的颜色。
String[] words = {"A ", "B ", "C ", "D ", "E ", "F "};
int newIndex = 0;
int oldIndex = -1;
PFont SansSerif;
String beat = "";
int x = 0;
void setup() {
size(700, 500);
background(255); // Changed to lighter background
SansSerif = createFont("SansSerif", 162);
textFont(SansSerif);
}
void draw() {
frameRate(.6);
background(255); // Changed to lighter background
// Get a random element from array
newIndex = int(random(words.length));
if (oldIndex > -1) {
beat = words[oldIndex] + words[newIndex];
int x = 150;
for (int i = 0; i < beat.length(); i++){
if(i == 0){
fill(0);
} else {
fill(150);
}
text(beat.charAt(i), x, 300);
x += 60;
}
println("old =", words[oldIndex] + " : " + "new =", words[newIndex] );
} else {
fill(0);
text(words[newIndex], 150, 300);
println("new =", words[newIndex]);
}
oldIndex = newIndex;
}