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)
   setPixel() getPixel() Trouble
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: setPixel() getPixel() Trouble  (Read 563 times)
mikky_m


setPixel() getPixel() Trouble
« on: Oct 14th, 2004, 11:31am »

hi was looking at some code available from http://www.lightcycle.org/
 
it uses getPixel(x,y,0);
   setPixel(x,y,c);
 
and so on. I tried running it in my Processing (68 Alpha) but every time it comes up with mistake about setPixel(), getPixel(). Can anyone suggest what might be causign it. As those applets are runnign fine on the http://www.lightcycle.org/ web page. Thanks a lot in advance.
 
 
TomC

WWW
Re: setPixel() getPixel() Trouble
« Reply #1 on: Oct 14th, 2004, 12:58pm »

In recent versions, these methods have been renamed get and set.  You can find them in the reference section under 'pixels'.
 
You have to be prepared for this with alpha software
 
fry


WWW
Re: setPixel() getPixel() Trouble
« Reply #2 on: Oct 14th, 2004, 9:00pm »

yeah, and in the next release we're gonna change em to just g() and s(). good luck finding em in the reference!
 
mikky_m


Re: setPixel() getPixel() Trouble
« Reply #3 on: Oct 15th, 2004, 12:45am »

Hi, well I've looked at get() and set() before I posted my initial question, and tried using them but still got nowhere. It doesnt come up with any error in code, but at the bottom black window it says  
at Temporary_7083_5169&World.get(Temporary_7083_5169.java:136) and hmm thats it. Any ideas what am I doing wrong? Thanks
 
 
 
//  Pollen 4 by Mike Davis
//  mike [at] lightcycle [dot] org
 
//  A short program for alife experiments.
//
//  Each cell is represented by a pixel on the display as well as an entry in
//  the Vector 'cells'.  Cells each have a run() method, which performs actions
//  based on the cell's surroundings.  Cells run one at a time (to avoid conflicts
//  like wanting to move to the same space) in random order.
 
World w;
Vector cells;
 
int spore1, spore2;
 
// set lower for smoother animation, higher for faster simulation
int runs_per_loop = 10000;
 
void setup()
{
  size(100, 400);
  noBackground();
  clearscr();  
 
  w = new World();
  cells = new Vector();
   
  spore1 = color(128, 172, 255);
  spore2 = color(64, 128, 255);
 
  // Add a bunch of cells at random places
  for (int i = 0; i < 2000; i++)
  {
    int cX = (int)random(width);
    int cY = (int)random(height);
    int c;
    if (random(1) < 0.5) c = spore1;
    else c = spore2;
    if (w.get(cX, cY) == 0)
    {
 w.set(cX, cY, c);
 cell a = new cell(cX, cY);
 cells.add(0, a);
    }
  }
}
 
void loop()
{
  line(0, height - 1, width - 1, height - 1);  // floor
  // Run cells in random order
  cell selected;
  for (int i = 0; i < runs_per_loop; i++)
  {
    selected = (cell)cells.elementAt(min((int)random(cells.size()), cells.size() - 1));
    selected.run();
  }
}
 
void clearscr()
{
  for (int y = 0; y < height; y++)
    for (int x = 0; x < width; x++)
 setPixel(x, y, 0);
}
 
class cell
{
  int x, y;
  cell(int x, int y)
  {
    this.x = x;
    this.y = y;
  }
  // Perform action based on surroundings
  void run()
  {
    // Fix cell coordinates
    while(x < 0) x+=width;
    while(x > width - 1) x-=width;
    while(y < 0) y+=height;
    while(y > height - 1) y-=height;
     
    // Cell instructions
    int myColor = w.get(x, y);
    if (myColor == spore1)
    {
 if (w.get(x - 1, y + 1) == 0 && w.get(x + 1, y + 1) == 0 && w.get(x, y + 1) == 0) move(0, 1);
 else if (w.get(x - 1, y) == spore2 && w.get(x - 1, y - 1) != 0) move(0, -1);
 else if (w.get(x - 1, y) == spore2 && w.get(x - 1, y - 1) == 0) move(-1, -1);
 else if (w.get(x + 1, y) == spore1 && w.get(x + 1, y - 1) != 0) move(0, -1);
 else if (w.get(x + 1, y) == spore1 && w.get(x + 1, y - 1) == 0) move(1, -1);
 else move((int)random(3) - 1, 0);
    }
    else if (myColor == spore2)
    {
 if (w.get(x - 1, y + 1) == 0 && w.get(x + 1, y + 1) == 0 && w.get(x, y + 1) == 0) move(0, 1);
 else if (w.get(x + 1, y) == spore1 && w.get(x + 1, y - 1) != 0) move(0, -1);
 else if (w.get(x + 1, y) == spore1 && w.get(x + 1, y - 1) == 0) move(1, -1);
 else if (w.get(x - 1, y) == spore2 && w.get(x - 1, y - 1) != 0) move(0, -1);
 else if (w.get(x - 1, y) == spore2 && w.get(x - 1, y - 1) == 0) move(-1, -1);
 else move((int)random(3) - 1, 0);
    }
  }
  // Will move the cell (dx, dy) units if that space is empty
  void move(int dx, int dy)
  {
    if (w.get(x + dx, y + dy) == 0)
    {
 w.set(x + dx, y + dy, w.get(x, y));
 w.set(x, y, 0);
 x += dx;
 y += dy;
    }
  }
}
 
//  The World class simply provides two functions, get and set, which access the
//  display in the same way as getPixel and setPixel.  The only difference is that
//  the World class's get and set do screen wraparound ("toroidal coordinates").
class World
{
  void set(int x, int y, int c)
  {
    while(x < 0) x+=width;
    while(x > width - 1) x-=width;
    while(y < 0) y+=height;
    while(y > height - 1) y-=height;
    setPixel(x, y, c);
  }
  int get(int x, int y)
  {
    while(x < 0) x+=width;
    while(x > width - 1) x-=width;
    while(y < 0) y+=height;
    while(y > height - 1) y-=height;
    return getPixel(x, y);
  }
}
 
void mousePressed()
{
  setup();
}
 
 
 
fjen

WWW
Re: setPixel() getPixel() Trouble
« Reply #4 on: Oct 15th, 2004, 6:49am »

hmm ... some quirks introduced by changing setPixel to set which confused the set - get functions inside the world class (specificly return get(..))
changed the code, should work now.
/F
Code:

 //  Pollen 4 by Mike Davis  
 //  mike [at] lightcycle [dot] org  
   
 //  A short program for alife experiments.  
 //  
 //  Each cell is represented by a pixel on the display as well as an entry in  
 //  the Vector 'cells'.  Cells each have a run() method, which performs actions  
 //  based on the cell's surroundings.  Cells run one at a time (to avoid conflicts  
 //  like wanting to move to the same space) in random order.  
   
 World w;  
 Vector cells;  
   
 int spore1, spore2;  
   
 // set lower for smoother animation, higher for faster simulation  
 int runs_per_loop = 10000;  
   
 void setup()  
 {  
   size(100, 400);  
   //noBackground();  
   clearscr();    
   
   w = new World();  
   cells = new Vector();  
     
   spore1 = color(128, 172, 255);  
   spore2 = color(64, 128, 255);  
   
   // Add a bunch of cells at random places  
   for (int i = 0; i < 2000; i++)  
   {  
     int cX = (int)random(width);  
     int cY = (int)random(height);  
     int c;  
     if (random(1) < 0.5) c = spore1;  
     else c = spore2;  
     if (w.wget(cX, cY) == 0)  
     {  
  w.wset(cX, cY, c);  
  cell a = new cell(cX, cY);  
  cells.add(0, a);  
     }  
   }  
 }  
   
 void loop()  
 {  
   line(0, height - 1, width - 1, height - 1);  // floor  
   // Run cells in random order  
   cell selected;  
   for (int i = 0; i < runs_per_loop; i++)  
   {  
     selected = (cell)cells.elementAt(min((int)random(cells.size()), cells.size() - 1));  
     selected.run();  
   }  
 }  
   
 void clearscr()  
 {  
   for (int y = 0; y < height; y++)  
     for (int x = 0; x < width; x++)  
  set(x, y, 0);  
 }  
   
 class cell  
 {  
   int x, y;  
   cell(int x, int y)  
   {  
     this.x = x;  
     this.y = y;  
   }  
   // Perform action based on surroundings  
   void run()  
   {  
     // Fix cell coordinates  
     while(x < 0) x+=width;  
     while(x > width - 1) x-=width;  
     while(y < 0) y+=height;  
     while(y > height - 1) y-=height;  
 
     // Cell instructions  
     int myColor = w.wget(x, y);  
     if (myColor == spore1)  
     {  
  if (w.wget(x - 1, y + 1) == 0 && w.wget(x + 1, y + 1) == 0 && w.wget(x, y + 1) == 0) move(0, 1);  
  else if (w.wget(x - 1, y) == spore2 && w.wget(x - 1, y - 1) != 0) move(0, -1);  
  else if (w.wget(x - 1, y) == spore2 && w.wget(x - 1, y - 1) == 0) move(-1, -1);  
  else if (w.wget(x + 1, y) == spore1 && w.wget(x + 1, y - 1) != 0) move(0, -1);  
  else if (w.wget(x + 1, y) == spore1 && w.wget(x + 1, y - 1) == 0) move(1, -1);  
  else move((int)random(3) - 1, 0);  
     }  
     else if (myColor == spore2)  
     {  
  if (w.wget(x - 1, y + 1) == 0 && w.wget(x + 1, y + 1) == 0 && w.wget(x, y + 1) == 0) move(0, 1);  
  else if (w.wget(x + 1, y) == spore1 && w.wget(x + 1, y - 1) != 0) move(0, -1);  
  else if (w.wget(x + 1, y) == spore1 && w.wget(x + 1, y - 1) == 0) move(1, -1);  
  else if (w.wget(x - 1, y) == spore2 && w.wget(x - 1, y - 1) != 0) move(0, -1);  
  else if (w.wget(x - 1, y) == spore2 && w.wget(x - 1, y - 1) == 0) move(-1, -1);  
  else move((int)random(3) - 1, 0);  
     }  
   }  
   // Will move the cell (dx, dy) units if that space is empty  
   void move(int dx, int dy)  
   {  
     if (w.wget(x + dx, y + dy) == 0)  
     {  
  w.wset(x + dx, y + dy, w.wget(x, y));  
  w.wset(x, y, 0);  
  x += dx;  
  y += dy;  
     }  
   }  
 }  
   
 //  The World class simply provides two functions, get and set, which access the  
 //  display in the same way as getPixel and setPixel.  The only difference is that  
 //  the World class's get and set do screen wraparound ("toroidal coordinates").  
 class World  
 {  
   void wset(int x, int y, int c)  
   {  
     while(x < 0) x+=width;  
     while(x > width - 1) x-=width;  
     while(y < 0) y+=height;  
     while(y > height - 1) y-=height;  
     set(x, y, c);  
   }  
   int wget(int x, int y)  
   {  
     while(x < 0) x+=width;  
     while(x > width - 1) x-=width;  
     while(y < 0) y+=height;  
     while(y > height - 1) y-=height;  
     return get(x, y);  
   }  
 }
   
 void mousePressed()  
 {  
   setup();  
 }
 
rich

amunre05 WWW Email
Re: setPixel() getPixel() Trouble
« Reply #5 on: Oct 23rd, 2004, 6:06pm »

I am continually having problems using set(), especially when incased in a class and being given variable arguments. Example:
 
Code:

Particle[]dots;
int MAX=300;
int i=0;
void setup(){
  size(350, 350);
  dots=new Particle[MAX];
}
void loop(){
  background(0);
  for (i=0;i<MAX;i++){
  dots[i]=new Particle(randPos(width),randPos(height));
  }
}
float randPos(float val){
  float myRandom=random(val);
  println(myRandom);
  return myRandom;  
}
class Particle{
  float x, y;//positioning for Particle
  //Constructor
  Particle(float x, float y){
    color colorVar=color(225,225,225);
    set(x,y,colorVar);
  }
}

 
Simple enough, right? Well it gives me lib/build/Temporary_4844_5038.java:24:5:24:21: Semantic Error: No method named "set" was found in type "Temporary_4844_5038$Particle".
 
Does anyone know why this happens?
 
fjen

WWW
Re: setPixel() getPixel() Trouble
« Reply #6 on: Oct 24th, 2004, 2:18am »

well .. set uses int as intput so change your code to:
 
set((int)x,(int)y,colorVar);
 
if you need mor precision you could as well do:
 
set( (int)round(x), (int)round(y), colorVar);
 
best, /F
 
Pages: 1 

« Previous topic | Next topic »