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 › Arrays, objects, classes, buttons.
Page Index Toggle Pages: 1
Arrays, objects, classes, buttons. (Read 401 times)
Arrays, objects, classes, buttons.
Jul 30th, 2008, 6:29pm
 
Hello to all and greeting to You, who created, develope and support  Proce55ing!
Proce55ing  is  Poetry.

I am newbie in arrays and selforganized Strings[] &
I have bad problem inside not simple code and that's why I'm inside the  «vicious circle».
The problem cosists of :

1)Arrays syntax:
a) please tell howto declare new object classes mixed with array;
2)Arrays consist of objects created by class:
a) Must  new objects to be declared before set up with  «[]»?
3)I try to create new object by button each time i push it, but I need
to add  it  inside : void mousePressed{ myclassobjectZZZ.mousepressed};
because I need each object to be manipulated that's why separated,  so I can't use static ints.
Maybe here I need create Strings[]???
4)Howto constrain the run of « for (int i ..... ) {
                                                   ...
                                                   myobjectclassZZZ[] = new myobjectclass();
                                                               ... «
           because I think it should be done when :void Button pressed { i=i++};
I've  read all  topics  about arrays, please tell what topic     is the  light way for me to out ???
5)  What I miss without Strings[]?
6) Or maybe the best way is Button pushed {
                           str = some “int” ++
                           cube(str) = new Cube;




main part of code of my problem :




Mcube cubeZZZ;                                     // - Class' object example
// For arrays                                                                 //   1?
int j =200;
int[] s = new int[j];
//public int i;
Mcube cube[];        // or Mcube[] cube                                                //   2?
void setup() {
 cubeZZZ = new Mcube(); //  ready object ex.
 // For arrays
 //String abc = str(s[i]);
 for(int  i = 0; i<200; i++) {                                       //   3?
   s[i]=i;
 
   cube[i] = new Mcube();
 }


void draw {

for(int  i = 0; i<100; i++) {
//... ?????                                                                      //   4?
   pushMatrix();
   translate(700,700,50);
   //cube[i].display();
   popMatrix();
 }



void mousePressed() {                                                
 mCubeZZZ.mousePressed();
// ?????
mCube[].mousePressed                                                 //   5?
}


/*
Some button pressed {                                                  //   6?
i= i++
}


*/






Re: Arrays, objects, classes, buttons.
Reply #1 - Jul 30th, 2008, 6:31pm
 
If it's necessary I'll post full code (classes, void mouse, camera).
Re: Arrays, objects, classes, buttons.
Reply #2 - Jul 30th, 2008, 7:52pm
 
1) OK
2) I think both are OK, but Mcube[] cube is more often used (grouping declaration parts).
3 - in code) Before the loop, you must create the array: cube = new Mcube[OBJECT_NB];
3 - question) If you need a variable number of objects (max size not known in advance), you must use an ArrayList (for example), which is extensible at will.
4 - question & 6 - code) I don't understand the question about constraining... Note that i=i++ isn't really necessary, i++ is self sufficient.
I fear I don't get the other questions.
Re: Arrays, objects, classes, buttons.
Reply #3 - Jul 31st, 2008, 1:08am
 
 PhiLho thank you for quick reply and excuse me for my not clear text.
I got answers [1-3], but 4-6 became mixed.
I think I almost understand evrything.
Could you give me the explanation one more time?
I would like to specify the things I didn't get :
1) What is [OBJECT_NB]?
2) How to use the variable of objects quantity in array or in ArrayList by button
(when it adds 1 object to declaration, to set up, to loop and 1 to further object's execution of smth( for example:
void mousePressed {
cube[].mousePressed } ...
3)Do I need to use redraw() after
Button pressed?  )    )
Re: Arrays, objects, classes, buttons.
Reply #4 - Jul 31st, 2008, 9:29am
 
OBJECT_NB is just a random name for a typical way of defining an array bound. Usually you put at the top of the sketch a number of such declarations:

final static int OBJECT_NB = 100;
final static int ITERATION_NB = 20;

then use it to declare the size of the array, as loop bound, etc.

Another way for loop bounds is to use arrayVariable.length where arrayVariable is just another generic name...
Eg. for (int i = 0; i < s.length; i++)

For ArrayList, you use the size() method (or an iterator):
for (int i = 0; i < al.size(); i++)

And you don't necessarily call redraw() unless you use noLoop(), because usually draw() call is often enough to refresh the screen in time.
Re: Arrays, objects, classes, buttons.
Reply #5 - Jul 31st, 2008, 8:45pm
 
    Thank you very much. I've got it! That's perfect!
Page Index Toggle Pages: 1