FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   ordered layers and stacks
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: ordered layers and stacks  (Read 257 times)
kevinP

Email
ordered layers and stacks
« on: May 25th, 2004, 11:22am »

Hi all,
 
I was working on a little thing where one has to stack objects in a particular order; to do this I need to bring the selected object to the top. I used java.util.Stack and this worked fine (console demo code below) but I'm wondering if this is the best way to do this. Also when I draw my objects I'm iterating over the stack something like this:
Code:

// myDots is the Stack
for(int i=0; i<myDots.size(); i++) {      
   ((Dot) myDots.get(i)).draw();
}
// for some reason the Iterator didn't work here

 
The Vector class (from which Stack is extended) has a "toArray" method; I thought that maybe it would be faster (for drawing) when I need all elements to first dump them en masse to an array and then iterate in the usual way over the array.
 
-K
 
Code:

// stack test - offers a nice little console demo of how Stack works
// stack extends Vector; (stores objects)
 
Stack myStack;
AnObject myObject;
int nbrElements;
 
void
setup()
{
 
  myStack = new Stack();
  for(int i=0; i < 6; i++) {
    myStack.push(new AnObject(i));
  }
  
  nbrElements = myStack.size();
  println("Stack contains " + nbrElements + " elements");
  for(int i=0; i < 2; i++) {
    AnObject tmp = (AnObject) myStack.pop();  
    println("  Popping top element (index #" + myStack.size() + ", value =  " + tmp.value + ")");
  }
  nbrElements = myStack.size();
  println("Stack now contains " + nbrElements + " elements");
  println();
 
  AnObject objectA = (AnObject) myStack.remove(2);
  print("Removing element at index #2 (value = " + objectA.value + "). ");   // prints "10"
  println("Stack now contains " + myStack.size() + " elements");
    
  AnObject objectB = (AnObject) myStack.remove(2);
  print("Again removing element at index #2 (value = " + objectB.value + ". ");   // prints "11"
  println("Remaining stack now contains " + myStack.size() + " elements");
  println();
  
  myStack.push(objectB);
  print("Pushing 2nd object from index #2 back onto stack. ");
  println("Stack now contains " + myStack.size() + " elements");
      
  Iterator it = myStack.iterator();
  println("Iterating over stack");
  while(it.hasNext()) {
    AnObject tmp = (AnObject) it.next();
    println("  Element with value: " + tmp.value);
  }
 
  int val = ((AnObject) myStack.get(1)).value;
  println("Getting element at index #1 (value = " + val + ")");
  println("Stack now contains " + myStack.size() + " elements");
    
  AnObject objectC = (AnObject) myStack.pop();
  println("Popping last element off stack; value = " + objectC.value);
 
}
 
class AnObject
{
  int value;
  
  AnObject(int myValue)
  {
    value = myValue;
  }
  
  void printValue()
  {
    println(value);
  }
}
« Last Edit: May 25th, 2004, 11:23am by kevinP »  

Kevin Pfeiffer
Pages: 1 

« Previous topic | Next topic »