Hi there,
I am new to Processing and created an applet which shows "students" according to a number in a data base.
In short, this is basically what happens:
Code:
[...]
// Percentage in data base
int eat = 6;
[...]
// Take it 10 times
int ec = eat;
numberofstudents = ec*10; // = 60
[...]
// Create 60 students...
for (int e=1; e<numberofstudents; e++) {
studentx[e] = random(220,465); // random value for x-coordinate
studenty[e] = random(115,335); // random value for y-coordinate
[...]
// ... at different possitions and then randomly move them
ellipse(studentx[e]+5, studenty[e]-5, 10, 10);
rect(studentx[e],studenty[e],10,20);
[...]
The students should be created once at the beginning and later when clicking on another country (like Canada). After the "student's creation" the student should move to random directions.
Unfortunatelly, the students don't move but are created at new positions in every frame.
I think I did something wrong with the array, but I can't find out.
Maybe someone can help me.
Here the complete programm code:
Code:// Data
int eat = 6; float mat = 0.8; String at = "Austria";
int eca = 5; float mca = 1.1; String ca = "Canada";
// Country by default: Austria
int ec = eat;
float mc = mat;
String country = at;
// Different things
String [] b = {at,ca};
int a;
PFont schrift;
int numberofstudents;
int s = 1;
void setup() {
size(800,600);
schrift = loadFont("ArialMT-12.vlw");
}
void draw() {
a=50;
background(255);
statemenu();
// Show data to verify the program works
fill(0);
textFont(schrift, 14);
text (country,400,500);
text (mc,500,500);
text (ec,600,500);
// End of Show data to verify the program works
if (mousePressed == true) {
if ( (mouseX>15) && (mouseX<175) ) {
if ((mouseY>56) && (mouseY<75)) { ec = eat; mc = mat; country = at; s=1; }
if ((mouseY>80) && (mouseY<99)) { ec = eca; mc = mca; country = ca; s=1; }
}
}
numberofstudents = ec*10;
float [] studentx = new float[numberofstudents];
float [] studenty = new float[numberofstudents];
// Students' place
stroke(0);
noFill();
rect(220,100,250,250);
for (int e=1; e<numberofstudents; e++) {
// Create new students
// Only when new country was chosen
if (s==1) {
studentx[e] = random(220,465);
studenty[e] = random(115,335);
if (e==numberofstudents) { s=2; }
}
frameRate(20);
// Keep students within borders
if (studentx[e]<221) {studentx[e]++;}
else if (studentx[e]>459) {studentx[e]--;}
else if (studenty[e]<102) {studenty[e]++;}
else if (studenty[e]>329) {studenty[e]--;}
// Move students randomly
else {
int rn1 = (int) random (2);
int rn2 = (int) random (2);
int rn3 = (int) random (2);
int rn4 = (int) random (2);
if (rn1 == rn2) {studentx[e]++;} else {studentx[e]--;};
if (rn3 == rn4) {studenty[e]++;} else {studenty[e]--;};
stroke(0);
fill(0);
ellipse(studentx[e]+5, studenty[e]-5, 10, 10);
rect(studentx[e],studenty[e],10,20);
}
}
}
void statemenu(){
for (int i=0; i<b.length; i++) {
a=a+24;
noFill();
rect(15,a-14,160,17);
fill(0);
textFont(schrift, 12);
text (b[i],20,a);
}
}