Ok this is my code so far....
When typing in a number you can see the the typed number as the width of the current rect.
Let's say you type in these numbers 10, 20, 30. Then you can see 3 rects right underneath each other. But when you now type in 25 this 4th rect should be 5 pixels smaller than the 3rd rect which is 30 pixels wide. But as soon as you type in a smaller number th rect is automatically as long as the highest number.
I dont get why because before you press Enter the correct size is shown in the window.
Because what I finally want to do is, that you can first of all type in many number and all rects have a different size. And when you Click the mouse the screen is deleted and the rects (and also the numbers) are sorted by size....
I hope you get what I mean.... :/ Can you help me again? I think we're close to solve the whole problem......thanks a lot
I thought that the for-loop inside the draw()-Function only cares for the last typed-in number....
Code:
ArrayList numbers;
void setup() {
size (400,400);
numbers = new ArrayList();
numbers.add(new Number());
}
void draw () {
for (int i = 0; i < numbers.size(); i++) {
fill(0);
Number n = (Number) numbers.get(i); // You have to cast back objects from a Collection
rect(20, i*10, n.getValue(), 5);
}
}
class Number implements Comparable {
StringBuilder buffer;
Number() {
buffer = new StringBuilder();
}
void update(char c) {
// Might check c before adding it
buffer.append(c);
}
int getValue() {
if (buffer.length() == 0)
return 0;
return Integer.valueOf(buffer.toString());
}
String toString() {
return buffer.toString();
}
public int compareTo(Object o) {
Number n = (Number) o;
int i1 = getValue();
int i2 = n.getValue();
return i1 == i2 ? 0 : (i1 > i2 ? 1 : -1);
}
}
void keyReleased() {
boolean bDisplay = false;
if (keyCode == ENTER) {
bDisplay = true;
println("");
}
else {
// Update latest number
Number n = (Number) numbers.get(numbers.size() - 1);
n.update(key);
print(key);
}
if (bDisplay) {
Collections.sort(numbers);
println(numbers);
numbers.add(new Number());
}
}