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.
Page Index Toggle Pages: 1
img.get (Read 2472 times)
img.get
Apr 20th, 2005, 11:54am
 
Hi there,
first of all thanks for this excellent processing beta!
can the arguments of img.get and image be variables after an IF statement?

I seem not able to run someting like this:
...
void draw() {
 xpos = xpos+ts;
 if(xpos > width && ypos < height) {
   ypos = ypos+ts;
   xpos = 0;
 }
  PImage img = loadImage("myfile.jpg");
  PImage tile = img.get(xpos, ypos, ts, ts);
  image(tile, xpos, ypos);
...

cheers,
p
Re: img.get
Reply #1 - Apr 20th, 2005, 12:43pm
 
That should work if xpos, ypos and ts are declared outside of draw() and setup().  Variables are declared when you first define them, saying what type they are (int, float, etc) and perhaps giving them a value.

e.g.

Code:


// declare three ints:
int xpos = 0;
int ypos = 0;
int ts = 10;

void setup() {
// you can modify xpos, ypos and ts
// in here because they were declared
// outside of setup() and draw()
}

void draw() {
// same here
}



If you declared them in setup, they won't be accessible in draw:

Code:


void setup() {
int xpos = 0;
int ypos = 0;
int ts = 10;
}

void draw() {
// you can not modify xpos, ypos and ts
// in here because they were declared
// inside setup()
xpos = 10; // this is an error
}



In general, things are accessible (in scope) if they were declared inside the same pair of curly brackets[ (a block).  Blocks can be nested (like your if statement - statements inside it are also inside of draw).

Whilst I'm here, it's worth pointing out that it's better to declare your img object outside of draw and setup, and load it in setup, because otherwise it will be loaded every frame. That could be very inefficient.

Hope that helps.

Exception
Reply #2 - Apr 20th, 2005, 3:18pm
 
Thanks.
I declared my xpos and ypos as floats... and the datatype is not allowedin the function where I was using them. darn... my fault.

I have also applied your efficiency suggestion, which is also stated in the reference: now I declare the image out of the functions, load it in setup and use it in draw. pretty fast!

I do have another problem now, find here my very first processing program:

<code>
// generate images on a tile-by-tile, renderview style, basis
// by paolo berto


int ts = 25; // tile size
int xpos = -ts;
int ypos = 0;
PImage img;
PImage tile;

 
void setup() {
 size(ts*32, ts*4); //800x100
 framerate(60);
 background(0);
 img = loadImage("my800x600image.jpg");
}

void draw() {
 
 xpos = xpos + ts;
 
 if(xpos > width  && ypos < height) {
   xpos = 0;
   ypos = ypos + ts;
 }

 //draw tile
 tile = img.get(xpos, ypos, ts, ts);
 image(tile, xpos, ypos, ts, ts);

}
</code>


Now, the program runs fine for the first raw of tiles, BUT as soon as the condition in the IF is true, the draw 'image' breakes saying "illegalArgumentException: width(0) and height (25)cannot be >= 0"
But in my program width is always = ts = 25

am I blind? any thought?

p
Re: img.get
Reply #3 - Apr 20th, 2005, 4:25pm
 
I think it must say:

xpos >= width

Otherwise your rightmost tile will start at the outer edge and thus have a width of 0 as there is no part of the image to be grabbed by the get() command.

Page Index Toggle Pages: 1