我是处理新手,遇到了一些问题。 我正在研究文本中词频的数据可视化,通过条形图显示。
对于示例代码,我用 200 个随机数字的序列替换了文本:当上下按键盘时,条形图会滚动,而屏幕的另一半应该显示每隔 200 个随机选取的数字之一。按下 R 键时或按下 H 时显示“Hello there”的文本。
在一些帮助下,我能够在屏幕上正确显示我的数字,但我不确定如何通过我需要在随机数字和文本之间进行切换的两种不同的按键交互来实现此功能。
我已经尝试过这样的操作,但我似乎无法使其适用于文本部分:
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”键时,您正在创建一个新字符串对象 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";
}
关于java - 处理中的 keyPressed 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56709460/