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 › ArrayList and color
Page Index Toggle Pages: 1
ArrayList and color (Read 1121 times)
ArrayList and color
Nov 22nd, 2009, 2:04am
 
Hi,

I am currently working on a small, dynamic colorManager class using processings color type and ArrayLists. Unfortunatelly its not as straightforward as I hoped it would be:
Code:

void addColor(color _c){
//add color to arraylist
colors.add(_c);
}
color getColor(int _id){
return (color) colors.get(_id);
}


in the get function I get the error Cannot cast from Object to int

I dont really see what I am doing wrong here, any help appreciated!

Thanks
Re: ArrayList and color
Reply #1 - Nov 22nd, 2009, 2:09am
 
It's an easy enough mistake to make.  Although Color looks like it's an object it actually just stores an int, which IIRC isn't directly compatible with ArrayLists.  This has been discussed recently on the forum, so a search may clarify.  Basically you may be better off working with normal Arrays, or create an Object that has a colour as a property.
Re: ArrayList and color
Reply #2 - Nov 22nd, 2009, 2:16am
 
thanks, I actually guessed that.- I might just write my own small color class then.- Thanks for the quick reply! Smiley
Re: ArrayList and color
Reply #3 - Nov 22nd, 2009, 3:51am
 
You just need an additional little sprinkle of magic:
Code:
color getColor(int _id){
return (color)(Integer) colors.get(_id);
}
Re: ArrayList and color
Reply #4 - Nov 22nd, 2009, 4:02am
 
ah, that looks good, I will try that, thanks!
Page Index Toggle Pages: 1