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 › 'Last items' arraylist
Page Index Toggle Pages: 1
'Last items' arraylist (Read 1444 times)
'Last items' arraylist
May 31st, 2010, 2:54pm
 
I have an arraylist of objects which display in draw(), but when the arrayList becomes larger than 48objects long I would like it to display only the last 48 on the list. My problem being that my objects are set an x & y position when originally created, so at the moment when new objects are added (over the 48 limit), the first few objects stop displaying but nothing moves to take their place.

Could I somehow swap each item's x position for the xPos of the previous item? - this code should run straight away & make my query clear instantly.


import processing.video.*;

Capture cam;
int blockSize;
int verticalIncreaser;
int blockNumber;
int pixelsVideoIndent = 10;
int displayColumnNumber;
int displayRowNumber;
 int tempCNumber;
 int tempRNumber;
 int numOfObjects = 0;
 
 float thumbnailScaleValue = 0.25;
 int thumbnailDownValue;
 int thumbnailAlongValue;

PhotoObject pixelPortrait;//my object.
ArrayList photoObjectsArrayList = new ArrayList();
color[] thisPortraitColours = new color[600];
color[] sampleColors = new color[600];

int showThumb = 0;


void setup() {
 size(800, 500, P2D);
 frameRate(5);
 background(0);
 cam = new Capture(this, 320, 240);
 blockSize = (cam.width/2)/20;
 noStroke();
}






void draw() {
 background(0);
 if (cam.available() == true) {
   cam.read();//read input video frame.
   cam.loadPixels();//load's new frame pixels.
     
   set(-70, pixelsVideoIndent, cam);//draws video thumbnail.
   fill(0);
   rect(0, 0, width/80, height);//frames video & pixel video screens.
   rect(0, (height/2)-10, width, (height/2)+10);
   rect(170, 0, width, height/2);
   
   stroke(120);
   strokeWeight(2);
   noFill();
   rect(pixelsVideoIndent, pixelsVideoIndent, 160, height*0.46);
   noStroke();
   
   
   pushMatrix();
   
     translate(0, height*0.48);//position pixel video to bottom left.
     
     for(int v = 0; v < cam.height-(blockSize/2); v = v+blockSize){//loop through rows.
       
       for(int a=cam.width/4; a<cam.width-(cam.width/4); a = a+(blockSize)){//loop through all columns.
           color pixelColor = cam.get(a, v);//samples pixel from columns.
           fill(pixelColor);
           rect(a-70, v+(pixelsVideoIndent), blockSize, blockSize);//draws average color blocks.
           blockNumber++;
           color tempColor = pixelColor;
           thisPortraitColours[blockNumber-1] = tempColor;
           displayColumnNumber = ((a-80)/5);
           
           tempCNumber = displayColumnNumber;
           tempRNumber = displayRowNumber;
           
           if(displayRowNumber < 30){
            displayRowNumber++;
           }else{
            displayRowNumber = 0;
           }
           
       }//end of columns.
       
     }//end of rows.
     
   popMatrix();
     
   stroke(120);
   strokeWeight(2);
   noFill();
   rect(pixelsVideoIndent, height/2, 160, height*0.48);
   noStroke();
     
     
     
   }//end if cam available.
 
   
   blockNumber = 0;
 
  if(photoObjectsArrayList.size() < 48){
   
   for (int n=0; n < photoObjectsArrayList.size(); n++){
     
    println("n = "+n);
    println("size = "+photoObjectsArrayList.size());
 
        showThumb++;
        println("showThumb = "+showThumb);
       
       PhotoObject currentObject = (PhotoObject) photoObjectsArrayList.get(photoObjectsArrayList.size()-showThumb);//-------Displ
ays thumbnails.
       currentObject.display();
   }
       
  }else{

     println("size = "+photoObjectsArrayList.size());
     
       for(int a=1; a < 48; a++){
         println("a = "+a);
         PhotoObject currentObject = (PhotoObject) photoObjectsArrayList.get(photoObjectsArrayList.size()-a);//-------Displays thumbnails.
       currentObject.display();
       }
       

       println("");
       
   }//end of loop through all objects

   
 showThumb = 0;
 
}//end of draw.





void keyPressed(){
 
 
   
   for(int i=0; i<sampleColors.length; i++){
     color temp = thisPortraitColours[i];
    sampleColors[i] = temp;
   
   }
 

 if(thumbnailAlongValue<12){//dynamically adjust the limit to fit more thumbnails/change scale as more go in?
   thumbnailAlongValue++;
 }else{
   thumbnailAlongValue = 1;
   thumbnailDownValue++;
 }
 
 photoObjectsArrayList.add(new PhotoObject(sampleColors, displayColumnNumber, displayRowNumber, floor(width*0.75)+(thumbnailAlongValue*(width/4)), (height*1.95)+(thumbnailDownValue*(height/2))));//create my object using current values in colors array.
 sampleColors = new color[600];

//  numOfObjects = photoObjectsArrayList.size();

}//end keypressed.


class PhotoObject{
 
 color[] pixelColorsArray = new color[600];//change this length dynamically?
 int colNumber;
 int rowNumber;
 int colPos;
 int rowPos;
 float thumbnailXPos;
 float thumbnailYPos;
 
 public PhotoObject(color[] tempPixelColorsArray, int tempColNumber, int tempRowNumber, float tempThumbnailXPos, float tempThumbnailYPos){
   
   pixelColorsArray = tempPixelColorsArray;
   colNumber = tempColNumber;
   rowNumber = tempRowNumber;
   thumbnailXPos = tempThumbnailXPos;
   thumbnailYPos = tempThumbnailYPos;
   
 }
 
 
 
 void display(){
  println("displaying");
  println(rowNumber+" "+colNumber);
 
  pushMatrix();
  scale(thumbnailScaleValue);//shrink thumbnails down.
 
  for(int i=60; i<pixelColorsArray.length; i++){

    if(i>30){
     colPos = floor(i/20);
     rowPos = i-(colPos*20);
    }
   
   
   
    pushMatrix();
    translate(thumbnailXPos, thumbnailYPos);//translates whole thumbnail object.
    fill(pixelColorsArray[i]);
    rect(rowPos*(blockSize)+pixelsVideoIndent, colPos*blockSize, blockSize, blockSize);//positions & draws pixels.
    popMatrix();
   
  }
 
  popMatrix();
   
 }//end of display.
 
}//end of class.
Re: 'Last items' arraylist
Reply #1 - Jun 1st, 2010, 9:39am
 
Hi Ladle,

What about something like this?

Code:
  int displayednum = 48;

 int init = (photoObjectsArrayList.size() < displayednum) ? 0 : photoObjectsArrayList.size() - displayednum;

 for (int n = init; n < photoObjectsArrayList.size(); n++){

 // draw stuff here

 }


edit: or this:  Roll Eyes

Code:
for(int n = photoObjectsArrayList.size()-1; n > photoObjectsArrayList.size() - displayednum && n > 0; n--) { 

Re: 'Last items' arraylist
Reply #2 - Jun 3rd, 2010, 12:49pm
 
Sorted. Thanks for that!
Page Index Toggle Pages: 1