We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Problems using array
Page Index Toggle Pages: 1
Problems using array (Read 617 times)
Problems using array
Nov 19th, 2006, 7:25pm
 
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);
}
}
Re: Problems using array
Reply #1 - Nov 20th, 2006, 5:58pm
 
1: Just because Java allows you to write all of your code on one line doesn't mean you should.

2: studentx and studenty are local to draw(). Because they don't exist outside draw() they don't get modified in the long term.

3: In for loop of the order (int i = 0; i < value; i++), i will never reach "value". Which is why "s" in your code will never have a value of 2.

4: The following code answers the problems above and offers a few other alterations.
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;
float [] studentx, studenty;
void setup() {
size(800,600);
frameRate(20);
schrift = loadFont("ArialMT-12.vlw");
setStudents();
}
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;
setStudents();
}
if(mouseY > 80 && mouseY < 99){
ec = eca;
mc = mca;
country = ca;
setStudents();
}
}
}
// Students' place
stroke(0);
noFill();
rect(220,100,250,250);
for (int e=1; e<numberofstudents; e++) {
// Move students randomly
studentx[e] += (int)random(-2, 2);
studenty[e] += (int)random(-2, 2);
studentx[e] = constrain(studentx[e], 221, 459);
studenty[e] = constrain(studenty[e], 102, 329);
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);
}
}
void setStudents(){
numberofstudents = ec*10;
studentx = new float[numberofstudents];
studenty = new float[numberofstudents];
for (int e=1; e<numberofstudents; e++) {
studentx[e] = random(220,465);
studenty[e] = random(115,335);
}
}
Re: Problems using array
Reply #2 - Nov 23rd, 2006, 3:55pm
 
Hello st33d,

thank you very much for looking at my program.

> 1: Just because Java allows you to write all of your code on one line doesn't mean you should.

Okay. I am very new to programming so I will remember that.

> 2: studentx and studenty are local to draw(). Because they don't exist outside draw() they don't get modified in the long term.

Okay, I see.

> 3: In for loop of the order (int i = 0; i < value; i++), i will never reach "value". Which is why "s" in your code will never have a value of 2.

Ahh, now I understand. You are right.

> 4: The following code answers the problems above and offers a few other alterations.

Thanks. I looked through the code and I think I now understand what you changed. For me, there is much to learn.
Page Index Toggle Pages: 1