处理中的 keyPressed 问题
Issues with keyPressed in Processing
我是 Processing 的新手,遇到了一些问题。
我正在研究文本中词频的数据可视化,通过条形图显示。
对于示例代码,我用 200 个随机数序列替换了我的文本:在键盘上上下按时条形图滚动,而屏幕的另一半应该显示所选择的 200 个数字之一每次按下 R 键或按下 H 时出现“你好”的文本。
在一些帮助下,我能够在屏幕上正确显示我的数字,但我不确定如何使用我需要在随机数和文本之间兼顾的两种不同的按键交互来实现这一点。
我试过这样,但我似乎无法将其用于文本部分:
String numbers[];
String randomnumber;
String h;
//PANNING VARIABLES
float yPan = 0;
boolean panUp = false;
boolean panDown = false;
void setup() {
fullScreen();
smooth(2);
concordance = new IntDict();
String numberString = "74,34,62,41,81,11,36,76,37,14,54,57,50,57,95,35,82,100,49,67,48,18,11,97,14,41,85,53,80,72,61,91,93,67,72,73,72,20,32,45,62,9,36,70,99,47,6,98,36,26,65,32,75,8,93,71,72,55,95,7,1,36,93,9,21,100,43,22,30,58,21,4,9,4,35,12,57,26,3,90,16,65,36,11,67,4,22,14,63,89,45,10,51,60,3,32,12,41,83,37,100,91,74,76,73,69,6,22,26,100,59,16,24,30,41,77,79,91,86,37,88,26,10,49,32,95,79,50,46,49,45,58,74,22,53,44,82,61,60,61,73,31,41,5,94,38,48,5,90,77,91,6,45,11,1,16,13,23,10,52,45,8,48,16,96,24,61,25,59,40,44,6,13,51,88,42,62,43,94,8,24,47,96,71,10,30,22,68,54,87,70,45,8,78,77,81,24,7,21,7";
String[] numbers = splitTokens(numberString, ",");
//FREQUENCY COUNT
for (int i = 0; i < numbers.length; i++) {
String s=(numbers[i].toLowerCase());
concordance.increment(s);
}
}
void draw() {
background(0);
concordance.sortValuesReverse();
String[] keys = concordance.keyArray();
for (int i = 0; i < keys.length; i++) {
String num = keys[i];
int frequency = concordance.get(num);
//DRAW GRAPH
fill(255);
rect(35, (((i*13)+9)+(yPan)), (frequency*2.2), 6);
fill(255);
textAlign(RIGHT);
textSize(13);
text(frequency, 30, ((i*13)+16)+(yPan));
fill(255);
textAlign(LEFT);
text(num, (frequency*2.2)+40, ((i*13)+16)+(yPan));
}
if (randomnumber!=null) {
fill(0);
rect(width/2, 0, width/2, height);
fill(255);
textAlign(TOP);
textSize(60);
text (randomnumber, width/2+10, 9, width/2, height);
}
if (h!=null) {
fill(0);
rect(width/2, 0, width/2, height);
fill(255);
textAlign(TOP);
textSize(60);
text (h, width/2+30, 9, width/2, height);
}
}
void keyPressed() {
//SCROLL GRAPH
if (keyCode == UP) {
if (panUp) {
yPan +=35;
}
panUp = true;
panDown = false;
}
if (keyCode == DOWN) {
if (panDown) {
yPan -=35;
}
panUp=false;
panDown = true;
//PICK AND SHOW A RANDOM NUMBER WHEN R IS PRESSED
} else if (key == 'R' || key == 'r') {
String numberString = "74,34,62,41,81,11,36,76,37,14,54,57,50,57,95,35,82,100,49,67,48,18,11,97,14,41,85,53,80,72,61,91,93,67,72,73,72,20,32,45,62,9,36,70,99,47,6,98,36,26,65,32,75,8,93,71,72,55,95,7,1,36,93,9,21,100,43,22,30,58,21,4,9,4,35,12,57,26,3,90,16,65,36,11,67,4,22,14,63,89,45,10,51,60,3,32,12,41,83,37,100,91,74,76,73,69,6,22,26,100,59,16,24,30,41,77,79,91,86,37,88,26,10,49,32,95,79,50,46,49,45,58,74,22,53,44,82,61,60,61,73,31,41,5,94,38,48,5,90,77,91,6,45,11,1,16,13,23,10,52,45,8,48,16,96,24,61,25,59,40,44,6,13,51,88,42,62,43,94,8,24,47,96,71,10,30,22,68,54,87,70,45,8,78,77,81,24,7,21,7";
String[] numbers = splitTokens(numberString, ",");
int index = int(random(numbers.length));
randomnumber = numbers[index];
} else if (key == 'H' || key == 'h') {
String h = "hello there";
}
}
你们非常亲密。
首先:
} else if (key == 'H' || key == 'h') {
// String h = "hello there"; -> delete this
h = "hello there";
}
之前,当您按下 "h" 键时,您会创建一个 new 字符串对象 h,然后它不会在任何地方使用。删除h.
前面的String
接下来,当您先按 R 然后按 H,然后再按 R 时,您仍然看到文本的原因是 h 的值不为空,因此它覆盖了 R 的结果。要解决此问题,每当您按下按钮时,将要绘制的值初始化为 null:
} else if (key == 'R' || key == 'r') {
//initialize h string to null
if (h != null) {
h = null;
}
String numberString = "74,34...";
String[] numbers = splitTokens(numberString, ",");
int index = int(random(numbers.length));
randomnumber = numbers[index];
} else if (key == 'H' || key == 'h') {
//initialize random number to null
if (randomnumber != null) {
randomnumber = null;
}
h = "hello there";
}
我是 Processing 的新手,遇到了一些问题。 我正在研究文本中词频的数据可视化,通过条形图显示。
对于示例代码,我用 200 个随机数序列替换了我的文本:在键盘上上下按时条形图滚动,而屏幕的另一半应该显示所选择的 200 个数字之一每次按下 R 键或按下 H 时出现“你好”的文本。
在一些帮助下,我能够在屏幕上正确显示我的数字,但我不确定如何使用我需要在随机数和文本之间兼顾的两种不同的按键交互来实现这一点。
我试过这样,但我似乎无法将其用于文本部分:
String numbers[];
String randomnumber;
String h;
//PANNING VARIABLES
float yPan = 0;
boolean panUp = false;
boolean panDown = false;
void setup() {
fullScreen();
smooth(2);
concordance = new IntDict();
String numberString = "74,34,62,41,81,11,36,76,37,14,54,57,50,57,95,35,82,100,49,67,48,18,11,97,14,41,85,53,80,72,61,91,93,67,72,73,72,20,32,45,62,9,36,70,99,47,6,98,36,26,65,32,75,8,93,71,72,55,95,7,1,36,93,9,21,100,43,22,30,58,21,4,9,4,35,12,57,26,3,90,16,65,36,11,67,4,22,14,63,89,45,10,51,60,3,32,12,41,83,37,100,91,74,76,73,69,6,22,26,100,59,16,24,30,41,77,79,91,86,37,88,26,10,49,32,95,79,50,46,49,45,58,74,22,53,44,82,61,60,61,73,31,41,5,94,38,48,5,90,77,91,6,45,11,1,16,13,23,10,52,45,8,48,16,96,24,61,25,59,40,44,6,13,51,88,42,62,43,94,8,24,47,96,71,10,30,22,68,54,87,70,45,8,78,77,81,24,7,21,7";
String[] numbers = splitTokens(numberString, ",");
//FREQUENCY COUNT
for (int i = 0; i < numbers.length; i++) {
String s=(numbers[i].toLowerCase());
concordance.increment(s);
}
}
void draw() {
background(0);
concordance.sortValuesReverse();
String[] keys = concordance.keyArray();
for (int i = 0; i < keys.length; i++) {
String num = keys[i];
int frequency = concordance.get(num);
//DRAW GRAPH
fill(255);
rect(35, (((i*13)+9)+(yPan)), (frequency*2.2), 6);
fill(255);
textAlign(RIGHT);
textSize(13);
text(frequency, 30, ((i*13)+16)+(yPan));
fill(255);
textAlign(LEFT);
text(num, (frequency*2.2)+40, ((i*13)+16)+(yPan));
}
if (randomnumber!=null) {
fill(0);
rect(width/2, 0, width/2, height);
fill(255);
textAlign(TOP);
textSize(60);
text (randomnumber, width/2+10, 9, width/2, height);
}
if (h!=null) {
fill(0);
rect(width/2, 0, width/2, height);
fill(255);
textAlign(TOP);
textSize(60);
text (h, width/2+30, 9, width/2, height);
}
}
void keyPressed() {
//SCROLL GRAPH
if (keyCode == UP) {
if (panUp) {
yPan +=35;
}
panUp = true;
panDown = false;
}
if (keyCode == DOWN) {
if (panDown) {
yPan -=35;
}
panUp=false;
panDown = true;
//PICK AND SHOW A RANDOM NUMBER WHEN R IS PRESSED
} else if (key == 'R' || key == 'r') {
String numberString = "74,34,62,41,81,11,36,76,37,14,54,57,50,57,95,35,82,100,49,67,48,18,11,97,14,41,85,53,80,72,61,91,93,67,72,73,72,20,32,45,62,9,36,70,99,47,6,98,36,26,65,32,75,8,93,71,72,55,95,7,1,36,93,9,21,100,43,22,30,58,21,4,9,4,35,12,57,26,3,90,16,65,36,11,67,4,22,14,63,89,45,10,51,60,3,32,12,41,83,37,100,91,74,76,73,69,6,22,26,100,59,16,24,30,41,77,79,91,86,37,88,26,10,49,32,95,79,50,46,49,45,58,74,22,53,44,82,61,60,61,73,31,41,5,94,38,48,5,90,77,91,6,45,11,1,16,13,23,10,52,45,8,48,16,96,24,61,25,59,40,44,6,13,51,88,42,62,43,94,8,24,47,96,71,10,30,22,68,54,87,70,45,8,78,77,81,24,7,21,7";
String[] numbers = splitTokens(numberString, ",");
int index = int(random(numbers.length));
randomnumber = numbers[index];
} else if (key == 'H' || key == 'h') {
String h = "hello there";
}
}
你们非常亲密。
首先:
} else if (key == 'H' || key == 'h') {
// String h = "hello there"; -> delete this
h = "hello there";
}
之前,当您按下 "h" 键时,您会创建一个 new 字符串对象 h,然后它不会在任何地方使用。删除h.
前面的String
接下来,当您先按 R 然后按 H,然后再按 R 时,您仍然看到文本的原因是 h 的值不为空,因此它覆盖了 R 的结果。要解决此问题,每当您按下按钮时,将要绘制的值初始化为 null:
} else if (key == 'R' || key == 'r') {
//initialize h string to null
if (h != null) {
h = null;
}
String numberString = "74,34...";
String[] numbers = splitTokens(numberString, ",");
int index = int(random(numbers.length));
randomnumber = numbers[index];
} else if (key == 'H' || key == 'h') {
//initialize random number to null
if (randomnumber != null) {
randomnumber = null;
}
h = "hello there";
}