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 & HelpPrograms › How to structure my programm
Page Index Toggle Pages: 1
How to structure my programm (Read 1382 times)
How to structure my programm
Sep 21st, 2009, 10:55am
 
Hi Guys, i am having some troubles thinking about a good way to structure my programm. Maybe you can give me some hints or pseudocode on how a good structure would look like. Where to create classes and how to organise them etc...

i will make some short example how my programm should work.

There is a blank screen. everytime i press a key it should check if the key was already pressed. If not it should create an ellipse with the letter in it.  Do i press a key and the letter already exists a second object should appear and the size variable of all these given letters should become +1.

that wouldnt be a problem i guess, but i dont know if i should make only one class or a class for every letter or classes in classes and so on. Cause after that i want some more possibilities for interaction and dont know how this could be easily done. How would i then check if any of the letters A is close to a Letter B  adn then do XX. or add +1 to all C letters then Space is pressed. How do i adress them.
I guess that wouldnt work if i create just one class.

I am a bit confused on how to structure it. I hope you understand and have some ideas.

Thank you! Smiley

Re: How to structure my programm
Reply #1 - Sep 21st, 2009, 11:12am
 
You definitely don't want multiple classes for these objects -- a single class is perfect when you have many objects which share functionality but differ in certain areas.

I think you will want a class that has a "charName" variable to store the character it represents.  When you generate the list of objects of that class, each instance should be given its own character.  This leads to the question: "how does the computer know the alphabet?"  It doesn't, unless there's a special Java function I'm unaware of, so you'll have to teach it:

char[] alphabet=new char{'a','b','c','d' ......etc.};
// now alphabet[0] == 'a',  and alphabet[25] == 'z'.

keyObject[] myObjects = new keyObject[26];

void setup(){
  for (int i = 0; i < myObjects.length; i++){
     myObject[i] = new keyObject(alphabet[i]);
     // translation: running from 0 to 25, create each keyObject with the corresponding letter from the alphabet[] list.
  }
}

class keyObject{
  char charName;
  int pressNum;
  keyObject(char passChar){
     charName = passChar;
     // this allows setup() to pass characters to each object.
  }
}

Once you have your 26 objects, you can trap keyPresses simply, like this:

void keyPressed(){
  for (int i = 0; i < alphabet.length; i++){
     if (Char.toLowerCase(key) == alphabet[i]){
        myObjects[i].pressNum ++;
     }
  }
}

the Char.toLowerCase('x') is a Java function that will help avoid problems with the shift key or caps lock.  "key" in this case is a character equal to whatever just got pressed, for instance you can add: println (key); to see the output reported unmodified.

There are a lot of other puzzles left in this project, but this should get you started.

--Ben
Re: How to structure my programm
Reply #2 - Sep 21st, 2009, 11:37am
 
Hey Ben, thanks for your help. I guess i have choosen a bad example but i thought pressing letters would make sense, but it was confusing. in my case i dont know the amout of different  objects to create.

To specify it. Lets think of a random group of people. Everybody can go to the application and enter the ID of his ID Card. (no need to help here, just think of it as an event that pass this ID)  

And now, it should create a class with this ID, lets say the bubble again with the ID showing. now everytime a different id is entered a new class should be created...
is the id already existing it should just create another instance of the class and add 1 to size. So if i entered my id 2 times, there are now two bubbles with my id and so one.

So the problem here again, i dont know how many objets will exist in the end, so i cant precreate them and turn them on and off.

but there is the part again where i get confused. what if it is all the same class with just a different ID variable,  how do i check one group of ID objects against another one... for example, if and object with id 01 touches one with id 04 do XX

Thats where i get lost.

Hope it doenst confuse you to much Smiley

Thanks for your help!
Re: How to structure my programm
Reply #3 - Sep 21st, 2009, 12:17pm
 
Ok.  The "if objects of id xxx are touching objects of id yy" suggests you might want to look into a hashmap...but to start, you can use a Vector or ArrayList -- both are arrays of flexible size.  E.g.:

Vector mythings = new Vector();
mythings.add(new thing(thingvariables));
for (int i=0;i<things.size();i++){
  ((thing)things.get(i)).update();
}
other useful commands:
things.remove(this);
things.clear();
...
and to check to see whether objects of id xxx are touching objects of id yy, run through them in a double loop.  (This is where hashmaps are more efficient.)  But in this example:
for (int i=0;i<mythings.size();i++){
 for (int j=i;j<mythings.size();j++){
   if (j is touching i) (tell object j and i about it);
 }
}
the 'j=i' is a simple optimization: don't check the inner loop against objects that were already checked in the outer loop.
--Ben
Re: How to structure my programm
Reply #4 - Sep 21st, 2009, 12:23pm
 
Ok, i guess  understand that part.

What about the whole structure? is this now all one class with different objects?or a different class for every id?

im sorry for asking all these quetsions but im try to understand it. Smiley
Re: How to structure my programm
Reply #5 - Sep 21st, 2009, 4:17pm
 
No problem --
a class is mostly useful for the creation of multiple instances with slight variations.  So your structure might look like:

Vector myobjects = new Vector(); // declare an (empty) vector to hold your objects

[setup stuff]

draw{
 update all your objects
}

keyPressed / mousePressed / whanot{
 add a new object with the appropriate variables
}

class object{
 declare class-owned variables
 object( accept passed variables here){
    put passed vars into appropriate class-owned variables
 }
 void update(){
    this is how each object updates itself
 }
}
Re: How to structure my programm
Reply #6 - Sep 21st, 2009, 4:58pm
 
ok, this is just simply creating a new instance with every ID entered, that makes sense to me.  

but lets say now there are 3 persons. person 1 and 2 enters their ID 1 time, so there now is a green and blue circle (id defines colour)
now person 3 enters his ID 3 times. so he creates 3 red circles...

how can i "group" or call only these 3 red ones. to check them against the blue one for example. Thats actually my main problem

or should i just create another class inside the main ID class that is a circle class and then create different number of circles? so when the ID is first entered a new ID Class instance is created and then when entering it a second or third  , i only count up the numbers of Circle class instances that draws the circles.

Does that sound logical?
Re: How to structure my programm
Reply #7 - Sep 21st, 2009, 5:05pm
 
You can make separate groups of multiple "circle" objects using separate Vectors:

Vector redCircles = new Vector();
Vector blueCircles = new Vector();
Vector yellowCircles = new Vector();

but you aren't going to escape the nested loop structure I mentioned in the post before last.  In this case:

for (all redCircles)
 check against blue and yellow circles

for (all blueCircles)
  check against yellow circles

I hope it is clear that this is the same structure as the double loop example with the "i" and "j" variables.  This a relatively inefficient way to deal with large numbers of objects, but it shouldn't slow you down, and it's relatively easy to code.  So, in the "i" and "j" example, the only part you need to add is a conditional: if these objects are not the same color, check their distances and see if they are colliding.  That way you could keep everything in a single Vector.

for (int i=0;i<mythings.size();i++){
  for (int j=i;j<mythings.size();j++){
     if ((j.color != i.color) && (j is touching i)) (tell object j and i about it);
   }
}
Re: How to structure my programm
Reply #8 - Sep 21st, 2009, 5:19pm
 
alright, i didnt understand all of it Smiley but i guess from here on i will try my best and start to make a basic programm. Thank you alot for your help!
Page Index Toggle Pages: 1