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
expand array (Read 623 times)
expand array
Sep 23rd, 2006, 11:33pm
 
Hello again,

I wrote this:

Code:

int ex;
int timer = 0;//60 or smaller --> outofbounds; 61 --> nullpointer
BG bg[] = new BG [frameCount];

void setup()
{
size(400,400);
background(255);
bg = new BG[frameCount];

framerate(30);


}

void draw()
{
//int timer = 0;
timer++;
drawBG();
circle();
}

void circle()
{
noStroke();
fill(0,255,0);
ellipse(ex,width/2,10,10);
ex++;
}


void drawBG()
{
// timer++;
bg[timer].copyBG();

if (timer<60){
bg[timer].copyBG();
}
else
{
bg[60].copyBG();
}
}


class BG
{
int sx,sy,swidth,sheight,dx,dy,dwidth,dheight;

BG()
{
sx=0; sy=0; swidth=width; sheight=height; dx=0; dy=0; dwidth=width; dheight=height;
}

void copyBG()
{
copy(sx,sy,swidth,sheight,dx,dy,dwidth,dheight);
}
}



and that:


Code:

int ex;
BG bg[] = new BG [frameCount];

void setup()
{
size(400,400);
background(255);
bg = new BG[frameCount];

framerate(30);
}

void draw()
{

drawBG();
circle();
}

void circle()
{
noStroke();
fill(0,255,0);
ellipse(ex,width/2,10,10);
ex++;
}

void drawBG()
{
//bg = expand(bg,frameCount);
if (frameCount<60)
{
bg[frameCount] = expand(bg[frameCount-1],frameCount);
//error: "type of right sub-expression java.lang.Object is //not assignable to the variable of type BG()"
//expand only with booleans[], bytes[], chars[], ints[], //floats[], or Strings[]

bg[frameCount].copyBG();
// bg[frameCount-1].copyBG();
}
else
{
bg[60].copyBG();
}
}

class BG
{

/*float [] sx=new float[0];
float [] sy=new float[0];
float [] swidth=new float[width];
float [] sheight=new float[height];
float [] dx=new float[0];
float [] dy=new float[0];
float [] dwidth=new float[width];
float [] dheight=new float[height];*/

int sx,sy,swidth,sheight,dx,dy,dwidth,dheight;

BG()
{
sx=0; sy=0; swidth=width; sheight=height; dx=0; dy=0; dwidth=width; dheight=height;
}

void copyBG()
{
copy(sx,sy,swidth,sheight,dx,dy,dwidth,dheight);
}
}



Intention: using the current status of the display window as background picture to keep the trace of a moving object (although "background" is within "draw()") until a certain point of time, then repeating just one frame (No. 60), so moving objects don´t leave trace anymore. I hope it is understandable, what i mean.

Problem: The frames are collected in an array that needs to be expanded with every loop.Till now i didn´t manage this (see code). Perhaps someone knows, why?
Re: expand array
Reply #1 - Sep 24th, 2006, 10:24pm
 
the expand() method only works with arrays of primitive values -- integers, floats, etc. Since your array is of BG *objects*, it cannot be resized at any point with expand() during the course of the program.  You could either create a very large array (and just you the part that you need at any given time) or else you could investigate using an ArrayList.  An ArrayList is a resizable ordered collection of objects.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

Using an ArrayList like an array looks like this:

Code:

// Declaring the arraylist
ArrayList plist = new ArrayList() ;

// Adding elements
for (int i = 0; i < 10; i++) {
plist.add(new Particle()) ;
}

// Accessing elements
for (int i = 0; i < plist.size() ; i++) {
Particle p = (Particle) plist.get(i) ;
p.run();
}


Full examples:
http://www.shiffman.net/teaching/the-nature-of-code/particles/
Re: expand array
Reply #2 - Sep 25th, 2006, 5:09am
 
ArrayList is useful when you need a list of items.. however, with more recent releases, expand() works on all types of Objects, you just have to cast the result of the expand, i.e.

BG[] items = (BG[]) expand(originalArray);
Re: expand array
Reply #3 - Sep 25th, 2006, 5:13am
 
Oops, I didn't realize that re: expand(). . . good to know!
Re: expand array
Reply #4 - Sep 26th, 2006, 12:29am
 
Thanks a lot for the replies!
Re: expand array
Reply #5 - Sep 28th, 2006, 10:25pm
 
I keep getting two error messages which i don´t understand:

Code:

int ex;
BG bg[] = new BG [1];

void setup()
{
 size(400,400);
 background(255);
 bg = new BG[1];
 framerate(30);
 }
 
void draw()
 {  int timer=frameCount;
    BG[] items = (BG[]) expand(bg, timer);
    println(items.length);//timer = items = 1
     
     if (timer<60)
     {
     items.copyBG();
//No accessible method with signature "copyBG()" was found in type "Temporary...BG[]"

     //items[timer].copyBG();
//java.lang.ArrayIndexOutOfBoundsException: 1
    }
    else if  (timer>=60)
    {
    items[60].copyBG();
    }
   //println(bg.length);
 }


class BG
{
 
int sx,sy,swidth,sheight,dx,dy,dwidth,dheight;
 
BG()
{
  sx=0; sy=0; swidth=width; sheight=height; dx=0; dy=0; dwidth=width; dheight=height;
  }
 
  void copyBG()
  {
    copy(sx,sy,swidth,sheight,dx,dy,dwidth,dheight);
    }
 }



java.lang.ArrayIndexOutOfBoundsException: 1
--> when this line is called: timer = frameCount = 1 (because first loop). That´s correct in my opinion. Why this OutOfBounds-error?

No accessible method with signature "copyBG()" was found in type "Temporary...BG[]"
--> What´s wrong with the class declaration?
Re: expand array
Reply #6 - Sep 29th, 2006, 12:24am
 
//No accessible method with signature "copyBG()" was found in type "Temporary...BG[]"

This error is due to the fact that "items" (i.e. the array as a whole) is not a BG object.  It is a list of BG objects.  You must call the copyBG() on an individual element of the array, indicated by an index value, i.e.:

Code:

items[0].copyBG(); // or some variable instead of 0


The other error occurs when you try to access index #60 of the array.  You'll notice at that time, the array is of length 60.  Since array indices start at 0, an array with length 60 has index values 0 through 59. There is no element #60!  In this case, you'd want to access element 59.  Or better yet, you can dynamically access the last element by asking for the length minus one.

Code:

items[items.length-1].copyBG();


Re: expand array
Reply #7 - Sep 29th, 2006, 9:56pm
 
ok, i understood that now:
Code:

items[items.length-1].copyBG();


i found out, that there is some fault in the class definition (copyBG() doesn´t work)and thought of a way without using a class. The following does at least half of what i want to (i.e. leave a trace first and move further without trace) - but the trace is lost!

Code:

int ex=0;
PImage b;

void setup()
{
 size(400,400);
 background(255);
 framerate(30);
 b = new PImage(width,height);
 b = get();
 smooth();
 }
 
void draw()
 {
    int timer=frameCount;
   
    if (timer<60)
     {  
    copy(0,0,width,height,0,0,width,height);
     }

    else if  (timer>=60)
    {
    image (b,0,0);
    }

  ex++;
  ellipse(ex,height/2,10,10);
 }


isn´t there any possibility to save the screen at a certain point of time and use this picture as background?

instead of
Code:

b = get();


i´d like to do someting like this:
Code:

b = copy(0,0,width,height,0,0,width,height);


which causes the error "An expression of type "void" is not valid in this context where a value is expected."


i also rewrote the class BG:
Code:

class BG
{
 
int sx,sy,swidth,sheight,dx,dy,dwidth,dheight;
 
BG(int ssx,int ssy,int sswidth,int ssheight,int ddx,int ddy,int ddwidth,int ddheight)
{
  sx=ssx; sy=ssy; swidth=sswidth; sheight=ssheight; dx=ddx; dy=ddy; dwidth=ddwidth; dheight=ddheight;
  }
   
  void copyBG()
  {
    copy(sx,sy,swidth,sheight,dx,dy,dwidth,dheight);
    }
 }


, so i should write something (global) like

Code:

BG bg[] = new BG (0,0,width,height,0,0,width,height);


to pass values to the variables inside the class.
BUT:

Semantic Error: The type of the right sub-expression, "Temporary_...$BG", is not assignable to the variable, of type "Temporary_...$BG[]".

Sorry for all these beginner questions!
Page Index Toggle Pages: 1