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 & HelpSyntax Questions › Expanding array of objects
Page Index Toggle Pages: 1
Expanding array of objects (Read 2683 times)
Expanding array of objects
Oct 30th, 2007, 12:43am
 
At the beginning of my code, I declare a new array of objects of the class 'drawing', but I want it to be empty:
Code:

drawing drawings[] = new drawing[0];

Later on, I want to add drawings. With arrays of integers or floats, I can just use the expand function to add one to the index of the array, and using the array.length I can than fill that new space.
But with objects it doesn't seem to work:
Code:

drawings = expand(drawings, drawings.length+1);
drawings[drawings.length] = new drawing();

"The type of right sub-expression, "java.lang.Object", is not assignable to the variable, of type "Temporary_6098_398$drawing[]"

This is the code for my class (no real contents yet, I just started converting some 'normal' code to this class):
Code:

class drawing{
float aX, bX, aY, bY;
float xPoints[] = new float[]{
};
float yPoints[] = new float[]{
};
boolean beingDrawn = true;
drawing(){

}
void display(){
for (int i = 1; i< xPoints.length; i++){
strokeWeight(3);
stroke(255);
line(xPoints[i-1], yPoints[i-1],xPoints[i],yPoints[i]);
}
}
}


Can someone tell me how I can get this to work? I just want a dynamic array of objects.
Re: Expanding array of objects
Reply #1 - Oct 30th, 2007, 1:00am
 
Hi, I would suggest using an ArrayList instead of an array.
They work pretty much the same but are a little easier to manipulate.

Here's a detailed example:
-----------------------------------------------------------
ArrayList arrayName = new ArrayList();

void setup() {
 size(400,400);
 background(0);
 
 arrayName.add(new className());
 
  println(arrayName.size()); //returns the length of the ArrayList
  println(arrayName.get(0)); //return the item at a certain position (since you stored a class, it returns some funky stuff!)
}
 
void draw() {
  for ( int i = 0; i<arrayName.size(); i++ ) {
   ((className)arrayName.get(i)).draw();  //calls the function over and over
 }
}

class className{
 int variable;
 
 className() {
   variable = int(random(10));
 }  
 
 void draw() {
   println(variable);
 }
}
-----------------------------------------------------------

To get all the method, visit: http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html

Cheers!
Re: Expanding array of objects
Reply #2 - Oct 30th, 2007, 1:02am
 
Thats very usefull, thank you!
Re: Expanding array of objects
Reply #3 - Oct 30th, 2007, 2:43pm
 
expand() works on arrays of objects as well, you just have to cast the values returned because the function returns Object[].

so in your code, that would look like:

drawings = (drawing[]) expand(drawings, drawings.length+1);

however, expanding by 1 is inefficient. it's better to keep a count of how many values are in there, and just double the size of the array (this is the default that expand will use if no size is specified).

drawings = (drawing[]) expand(drawings);
drawings[drawingCount++] = new drawing();

ArrayList works fine to, but using arrays is faster in cases where you have a lot of values. for your particular code, the difference will not be apparent.
Re: Expanding array of objects
Reply #4 - Nov 4th, 2007, 1:39pm
 
I know what typecasting does, but could you maybe tell me what this syntax exactly does?
Code:

drawings = (drawing[]) expand(drawings, drawings.length+1);

I mean; I know what the result will be, but what would it say if you translate it to English?  This might help me understand it better so I can use this sort of things in other projects as well.
Re: Expanding array of objects
Reply #5 - Nov 4th, 2007, 1:47pm
 
take array named "drawings" and expand it's length to be 1 longer than before ("drawings.length+1"). the result will be passed back as Object[], so cast it to be of type "drawing[]". assign the result to the variable named "drawings" ("drawings = ").

clear?
Re: Expanding array of objects
Reply #6 - Nov 4th, 2007, 1:51pm
 
I know see I got confused by my own variables names: drawing vs. drawingS

Thank you :)
Re: Expanding array of objects
Reply #7 - Apr 17th, 2008, 5:57pm
 
Hey, I'm trying to do something very similar, but I keep getting an ArrayIndexOutOfBoundsException.

I thought this would work...
Code:

import processing.opengl.*;

float radius;
PulseCircle pulse[] = new PulseCircle[1];
PImage pulseCircleImg;
int PulseCircleCount = 1;

void setup() {
size(400,400,OPENGL);
hint( ENABLE_OPENGL_4X_SMOOTH );
textureMode(NORMALIZED);
pulseCircleImg = loadImage( "glowCircleThin.png" );
}

void draw() {
background(0);

translate(width/2, height/2);

if( keyPressed ) {
pulse = (PulseCircle[]) expand(pulse, pulse.length+1);
pulse[pulse.length] = new PulseCircle(0,0);

//pulse.start(0,0);
println(pulse.length);

}


//pulse.update();
//pulse.display();

}


class PulseCircle {
float x, y;
float pulseRadius;
boolean on = false;

// Constructor
PulseCircle(float xpos, float ypos) {
x = xpos;
y = ypos;
println("PulseCircle");
}

void start(float xpos, float ypos) {
x = xpos;
y = ypos;
on = true;
pulseRadius = 0;
println("Start");
}

void update() {
if (on == true) {
pulseRadius += 5;
println("Update : True");
}
if (pulseRadius > 500) {
pulseRadius = 0;
on = false;
println("Update : False");
}
}

void display() {
if (on == true) {
pushMatrix();
beginShape();
texture(pulseCircleImg);
vertex(-(pulseRadius), -(pulseRadius), 0, 0, 0);
vertex((pulseRadius), -(pulseRadius), 0, 1, 0);
vertex((pulseRadius), (pulseRadius), 0, 1, 1);
vertex(-(pulseRadius), (pulseRadius), 0, 0, 1);
endShape();
popMatrix();
//fill(255);
//ellipse(0,0,pulseRadius,pulseRadius);
println("Display");
}
}
}
Re: Expanding array of objects
Reply #8 - Apr 17th, 2008, 6:05pm
 
Arrays are 0-indexed, so if it's 3 elements long, the last element is number 2.
So pulse[pulse.length] = new PulseCircle(0,0);  needs to be
pulse[pulse.length-1] = new PulseCircle(0,0);
Page Index Toggle Pages: 1