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 › generating a flexible grid - Part 1
Page Index Toggle Pages: 1
generating a flexible grid - Part 1 (Read 423 times)
generating a flexible grid - Part 1
Jul 29th, 2007, 2:25am
 
Hi
I'm having a problem with the below code and hopefully someone has ideas/solutions.  I adapted the code from elsewhere.  It generates a grid that is both flexible with respect to resolution and color. Those aspects work fine.  However, when you place the mouse over any cell and click, the cell "darkens"(for any better word.  Also another cell situated somewhere on the screen also darkens.  So
1) I don't understand this behavior - what in the code says to "darken" a cell that is clicked on - as well as this other mystery cell.
2) I would actually like to have the cell - or cells - that are clicked on darken - those cells would then be used  as seed cells for some simulations.
3) are the cells That I'm clicking on actually changing color value - or is it something else.

The code is too long to fit in one post so I'm breaking it in half - First half is part 1 - Second half is part 2.  Sorry about this.

In any case  - any help would be appreciated
------------------------------------------------
import java.awt.datatransfer.*;

int [] pixmap;
int numColors = 3;
color [] toneMap = new color[numColors];  
int [] populationCount = new int[numColors];
float [] populationPerc = new float[numColors];
int res = 16;
int step = 20;

Slider srOff = new Slider("Red Contrast ",100,450,.6,-50,100);
Slider sgOff = new Slider("Green Contrast ",100,470,.5,-50,100);
Slider sbOff = new Slider("Blue Contrast ",100,490,.5,-50,100);

Slider refR = new Slider("Red",100,390,.3,0,255);
Slider refG = new Slider("Green",100,410,.1,0,255);
Slider refB = new Slider("Blue",100,430,0,0,255);
Slider gridSize = new Slider("Resolution",100,370,.2,5,50);

PImage tile = new PImage(res,res);
color refColor = color(130,60,0);
PFont fontA;
boolean outOfGamut = false;

Vector controls = new Vector();
void setup(){
 controls.add(srOff);
 controls.add(sgOff);
 controls.add(sbOff);
 controls.add(refR);
 controls.add(refG);
 controls.add(refB);
 controls.add(gridSize);
 
 PFont fontA = loadFont("arial.vlw");
 textFont(fontA, 12);
 textAlign(RIGHT);
 size(550,550);

 toneMap[0] = refColor;
 toneMap[1] = refColor;
 
 setupArrays();  
 mouseDragged();
}
void setupArrays(){  
 pixmap = new int[res*res];
   for (int i=0; i < res; i++){
     for (int j=0; j < res; j++){
       pixmap[j*res + i] = 0;
     }
   }
 tile = new PImage(res,res);
}
void draw(){
 stroke(0);
 background(refColor);
 for (int i=0; i < width/res; i++){
   for(int j =0; j < height/res; j++){
     image(tile,i*res,j*res);
   }
 }
 step = (width/1)/res;
 
 for (int i=0; i < res; i++){
 for (int j=0; j < res; j++){
   fill(toneMap[pixmap[j*res + i]]);
     rect(i*step,j*step,step,step );
 }
 }
 for (int i =0; i < controls.size(); i++){
   control c = (control)controls.elementAt(i);
   c.draw();
 }
}
void mouseDragged(){
 int newColor;
 int oRes = res;
 if (mouseButton == LEFT) newColor = 1;
 else newColor = 0;
 
 int ax = mouseX/step;
 int ay = mouseY/step;
 if (ax < res && ay < res && ax >= 0 && ay >= 0){
   pixmap[ay*res + ax] = newColor;
   pixmap[ax*res + ay] = newColor;
 }
   for (int i =0; i < controls.size(); i++){
   control c = (control)controls.elementAt(i);
   c.handleInput();  
 
 refColor = color(refR.getValue(), refG.getValue(), refB.getValue());
   //this is a bad hack for a missing actionlister interface. Need to fix this.
   res = gridSize.getValue();
   if (oRes != res){
     setupArrays();
   }  
 }
 updateRatio();  
}
void mousePressed(){
 mouseDragged();
}
void keyPressed(){
     BufferedImage img=new BufferedImage(res, res, BufferedImage.TYPE_INT_RGB);
     img.setRGB(0, 0, res, res, tile.pixels, 0, res);
     
     ImageSelection imgSel = new ImageSelection(img);
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);    
 }  
void updateRatio(){
 
 for (int i=0; i < numColors; i++){
  populationCount[i] = 0;
 }
 for (int i=0; i < res; i++){
 for (int j=0; j < res; j++){
   populationCount[pixmap[j*res + i]]++;
 }
 }
 for (int i=0; i < numColors; i++){
   populationPerc[i] = (float)populationCount[i]/(res*res);
 }
//---------------------------------end of Part 1
Page Index Toggle Pages: 1