|
Author |
Topic: color problem (Read 441 times) |
|
inadaze
|
color problem
« on: Dec 20th, 2004, 4:17pm » |
|
hi, I cannot figure out what is wrong with this code: //Superclass class Shape{ int Xpos; int Ypos; int shapeSize; color shapeColor; } //Subclass Circles class Circle extends Shape{ Circle(int Xpos,int Ypos,int shapeSize, int shapeColor){ this.Xpos = Xpos; this.Ypos = Ypos; this.shapeSize = shapeSize; this.shapeColor = shapeColor; } void draw(){ noStroke(); fill(this.shapeColor); ellipse(this.Xpos,this.Ypos,this.shapeSize,this.shapeSize); } } //============================================ void setup(){ background(0); size(500,500); ellipseMode(CENTER_DIAMETER); rectMode(CENTER_DIAMETER); } Circle c1 = new Circle(100,100,10,color(100,100,100)); void draw(){ c1.draw(); } Before I add the color parameter to the circle constructor it works, but when I add it, I get a strange java.lang.NullPointerException. Any idea? Thanks Jay
|
|
|
|
sspboyd
|
Re: color problem
« Reply #1 on: Dec 20th, 2004, 5:14pm » |
|
I am not sure why adding the color var would pooch it but splitting up the c1 declaration and initialisation makes it work. c1 can be declared outside of setup to make it global, but it needs to be initialised within setup (or some other code block if it makes sense) for it to 'work'. I don't remember off the top of my head why this is the case though. Time to review my oop books. steve Code:Circle c1; void setup(){ background(0); size(500,500); ellipseMode(CENTER_DIAMETER); rectMode(CENTER_DIAMETER); c1 = new Circle(100,100,10,color(100,100,100)); } |
|
|
« Last Edit: Dec 20th, 2004, 5:20pm by sspboyd » |
|
gmail.com w/ sspboyd username
|
|
|
toxi
|
Re: color problem
« Reply #2 on: Dec 20th, 2004, 5:22pm » |
|
by now i think it'd be safe to vote this the no. 1 "newbie" problem and maybe it should find its way in the FAQ someone was compiling once. here's a general rule of thumb to stay safe: "do not create objects or call functions from outside code blocks" the general order of execution of a sketch is: 1) everything outside code blocks/functions (e.g. declaration of variables) 2) call to setup() 3) call to draw() or loop() 4) if there's a loop() defined, go to step 3) it's not very obvious, but in your particular example the call to the constructor function of the Circle class is executed before everything else in your code, even before setup(). the constructor call also includes a call to the color() function, whereas this function is only available starting at step 2 (i.e. inside defined functions). that is because the colour conversion is also dependent on the current colour mode and resolution used. all of those settings are not yet initialized to their defaults before setup() has been executed. if you're still stumped a bit more detailed explanations info is also in these threads: 1 and 2
|
http://toxi.co.uk/
|
|
|
TomC
|
Re: color problem
« Reply #3 on: Dec 20th, 2004, 5:53pm » |
|
on Dec 20th, 2004, 5:22pm, toxi wrote:by now i think it'd be safe to vote this the no. 1 "newbie" problem and maybe it should find its way in the FAQ someone was compiling once. |
| * TomC hangs his head in shame... I'm doing this, and contributions are welcome. All I've got at the moment are forum links (many of these are from technotes already). I am also preparing simple overviews of Vector, Hashtable and String.
|
|
|
|
fry
|
Re: color problem
« Reply #4 on: Dec 21st, 2004, 2:27am » |
|
also fyi.. in 70+ i'm trying to minimize the headaches that come from doing stuff outside code blocks.. i.e. the colorMode() when you're outside setup() is simply rgb/255 and so it'll actually work. but yes, it's a faq item that would be cool to have noted along with TomC's String/Vector/Hashtable stuff. hm, maybe this should even be part of the docs.
|
|
|
|
inadaze
|
Re: color problem
« Reply #5 on: Dec 21st, 2004, 5:38pm » |
|
hi, I think I understand the order. It works anyway... Thanks Jay
|
|
|
|
|