FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   newbie program not working
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: newbie program not working  (Read 263 times)
whiteflea


newbie program not working
« on: Aug 25th, 2004, 11:20pm »

why?
 
I like to fill an array with random values and output lines with these values.
 
cheers
whiteflea
 
Code:

 
oid setup() {
  size(300,300);
  background(255);
}
 
 
 
 
  int[] zahlarray = new int[height/2];
  for (int i=0; i<height/2; i++) {
  zahlarray[i]= int(random(width));
}
 
 
 
void loop() {
 for (int i=0; i<height/2; i++) {
    line(0,i*2,zahlarray[i],i*2);
  }
}
 
 



==================================
Software is mindcontrol - get some
==================================
Shut up or I will replace you with
a very small processing script


exclamationmark period


Re: newbie program not working
« Reply #1 on: Aug 26th, 2004, 3:28am »

Code:

//in order to access zahlarray from setup and loop
//the variable scope must be outside both setup and loop
//therefore it must be declared outside both setup and loop routines
int[] zahlarray;
 
void setup() {
  size(300,300);
  background(255);
  zahlarray = new int[height/2];
  for (int i=0; i<height/2; i++) {
    zahlarray[i]= int(random(width));
  }
}
 
void loop() {
  for (int i=0; i<height/2; i++) {
    line(0,i*2,zahlarray[i],i*2);
  }
}
« Last Edit: Aug 26th, 2004, 3:36am by exclamationmark period »  
whiteflea


Re: newbie program not working
« Reply #2 on: Aug 26th, 2004, 5:31pm »

works like a charm! thank you.
If you remove the delay() it will flicker badly bcause the background refresh in the drawarray function. does anybody have an ideo to prevent this?  
thx
whiteflea
 
 
 
Code:

 
//bubbleSort
 
int[] zahlarray;
int buffer;
int halfheight;
 
void setup() {
  size(250,250);
  halfheight=width/2;
  zahlarray = new int[halfheight];
}
 
void loop() {
}
 
void sort() {
  for (int i = halfheight-1; 0 <= i; i--) {
    drawred(i);
    for (int j = 0; j < i; j++) {
 drawblue(j);
 if ( zahlarray[j + 1] < zahlarray[j] ) {
   buffer=zahlarray[j];
   zahlarray[j]=zahlarray[j+1];
   zahlarray[j+1]=buffer;
   delay(2);
 }
    }
    drawarray();
  }
}
 
void drawarray () {
  background(255);
  for (int i=0; i<halfheight-1; i++) {
    stroke(0);
    line(0,i*2,zahlarray[i],i*2);
  }
}
 
void drawred(int i) {
  stroke(255);
  line(0,i*2+3,width,i*2+3);
  stroke(255,0,0);
  line(0,i*2+1,width,i*2+1);
}
 
void drawblue(int y) {
  stroke(255);
  line(0,y*2-3,width,y*2-3);
  stroke(0,0,255);
  line(0,y*2+1,width,y*2+1);
}
 
void keyReleased() {
  sort();
}
 
void mouseReleased() {
  makearray();
  drawarray();
}
 
void makearray() {
  for (int i=0; i<halfheight; i++) {
    zahlarray[i]= int(random(1,width));
  }
}
 
 



==================================
Software is mindcontrol - get some
==================================
Shut up or I will replace you with
a very small processing script


TomC

WWW
Re: newbie program not working
« Reply #3 on: Aug 26th, 2004, 6:35pm »

The main loop happens at a fixed framerate (set it with framerate). However the event functions (in your case mousePressed() and keyReleased()) happen in real-time, multiple times per frame if needed/possible.
 
You should avoid doing 'work' in those functions, because they might write to pixels halfway through a refresh.  The contents of the applet only change at the end of a loop.
 
The solution here is probably to check inside loop if the mouse was pressed, and if the key was released.  There are boolean variables mousePressed and keyPressed to do this.  Then you can do the work inside loop, and it shouldn't flicker.
 
Hope that made sense.
« Last Edit: Aug 26th, 2004, 6:37pm by TomC »  
Pages: 1 

« Previous topic | Next topic »