Sign in to follow this  
hivemind

Java/Processing Question

Recommended Posts

hivemind    122
hi, I've been trying to learn processing/java and like most people I'm waiting for Daniel Shiffman's book to come out but until then I'm trying to work out as much as possible on my own. I found this code on the net that someone posted. Its a water simulation. I tried to get it to go fullscreen and add a background image to get a handle on the syntax. The code is fairly simple to understand but why my additions will not work is not. can someone help me fix this code to go full screen and display a background image? color col; int i,j,g; float osc=0; int x=0; int y=0; float c=0; float newmap[]=new float[width*height]; float currentmap[]=new float[width*height]; float prevmap[]=new float[width*height]; //my addition-changed 500*500 on the maps to width*height void setup(){ size(screen.width, screen.height,P3D); loadPixels(); //colorMode(RGB,2.0,1.0,1.0); //commented out colormode for PImage noCursor(); PImage w; w = loadImage("Worn wall.jpg"); image(w, 0, 0); //My addition- changed 500*500 to screen.width and screen.height //added PImage code //get a null pointer exception on image(w, 0, 0) why? for(g=0;g<width*height;g++){ currentmap[g]=0; newmap[g]=0; } } float lu(int x, int y, float array[]){ // look up a table return(array[(y*width)+x]); } void draw(){ x=mouseX; y=mouseY; osc+=.2; splash(x,y,.6); if(mousePressed); line(mouseX, mouseY, pmouseX, pmouseY); for(i=155;i>100;i--){ for(j=100;j<155;j++){ // currentmap[(width*j)+i]=.7;// } } //splash(160,100,100.,1) for(j=2;j<height-1;j++){ for(i=1;i<width-1;i++){ newmap[i+(j*width)]=.99* ((( lu(i+1, j, currentmap)+ lu(i-1, j, currentmap)+ lu(i, j+1, currentmap)+ lu(i, j-1, currentmap)+ lu(i+1, j+1, currentmap)+ lu(i+1, j-1, currentmap)+ lu(i-1, j+1, currentmap)+ lu(i-1, j-1, currentmap))/4.)-lu(i,j,prevmap)); // newmap[i+(j*width)]= newmap[i+(j*width)]- (newmap[i+(j*width)]*16); } } for(g=400;g<(width*height)-2222;g++) { // c=(newmap[g+width+1]-newmap[g-width-1]); // pixels[g]=color(0,0,atan2(.15,c)); pixels[g]=color((.1)*(1.-abs((newmap[g]))),(.7)*(.6-abs(newmap[g])), 1.-abs(newmap[g])); } arraycopy(currentmap,prevmap); arraycopy(newmap,currentmap); // currentmap=newmap; updatePixels(); } void splash(int x, int y, float power){ int sqsize=3; //intensity float a,b; if(x>=sqsize && y>=sqsize && x<width-sqsize && y<height-sqsize){ for(int xx=-sqsize;xx<sqsize;xx++){ for(int yy=-sqsize;yy<sqsize;yy++){ a=xx; b=sqsize; currentmap[((y+yy)*width)+x+xx]-=power;//(power-((a/b)*power)); } } } }

Share this post


Link to post
Share on other sites
DevFred    840
Code is much easier to read if you put [ source ] [ /source ] around it, like this:


color col;
int i,j,g;
float osc=0;
int x=0;
int y=0;
float c=0;

float newmap[]=new float[width*height];
float currentmap[]=new float[width*height];
float prevmap[]=new float[width*height];
//my addition-changed 500*500 on the maps to width*height




void setup(){
size(screen.width, screen.height,P3D);
loadPixels();
//colorMode(RGB,2.0,1.0,1.0);
//commented out colormode for PImage
noCursor();
PImage w;
w = loadImage("Worn wall.jpg");
image(w, 0, 0);
//My addition- changed 500*500 to screen.width and
screen.height
//added PImage code
//get a null pointer exception on image(w, 0, 0) why?



for(g=0;g<width*height;g++){
currentmap[g]=0;
newmap[g]=0;
}
}

float lu(int x, int y, float array[]){ // look up a table
return(array[(y*width)+x]);
}


void draw(){
x=mouseX;
y=mouseY;
osc+=.2;
splash(x,y,.6);

if(mousePressed);
line(mouseX, mouseY, pmouseX, pmouseY);
for(i=155;i>100;i--){
for(j=100;j<155;j++){
// currentmap[(width*j)+i]=.7;//
}
}
//splash(160,100,100.,1)
for(j=2;j<height-1;j++){
for(i=1;i<width-1;i++){

newmap[i+(j*width)]=.99*
(((
lu(i+1, j, currentmap)+
lu(i-1, j, currentmap)+
lu(i, j+1, currentmap)+
lu(i, j-1, currentmap)+
lu(i+1, j+1, currentmap)+
lu(i+1, j-1, currentmap)+
lu(i-1, j+1, currentmap)+
lu(i-1, j-1, currentmap))/4.)-lu(i,j,prevmap));
// newmap[i+(j*width)]= newmap[i+(j*width)]- (newmap[i+(j*width)]*16);
}
}

for(g=400;g<(width*height)-2222;g++) {

// c=(newmap[g+width+1]-newmap[g-width-1]);
// pixels[g]=color(0,0,atan2(.15,c));

pixels[g]=color((.1)*(1.-abs((newmap[g]))),(.7)*(.6-abs(newmap[g])), 1.-abs(newmap[g]));
}

arraycopy(currentmap,prevmap);

arraycopy(newmap,currentmap);
// currentmap=newmap;


updatePixels();
}




void splash(int x, int y, float power){
int sqsize=3; //intensity
float a,b;

if(x>=sqsize && y>=sqsize && x<width-sqsize && y<height-sqsize){
for(int xx=-sqsize;xx<sqsize;xx++){
for(int yy=-sqsize;yy<sqsize;yy++){
a=xx;
b=sqsize;
currentmap[((y+yy)*width)+x+xx]-=power;//(power-((a/b)*power));

}
}
}
}



Now, if you post your complete code (which class contains this code?) with correct intendation, i bet someone will help you ;-)

Share this post


Link to post
Share on other sites
ForD    122
That probably is the complete code, Processing is a very loose programing language that doesn't emphasize much structure. Its mostly ment for artists and musicians who don't know how to program.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this