| Author |
Problem in passing color as a parameter
|
Punit Jain
Ranch Hand
Joined: Aug 20, 2011
Posts: 702
|
|
Hello everyone, below code is for bounce multiple balls, my code is working correctly, but when i m trying to add Color also as a parameter, it not changing the color of the balls, code is:
Point.java
Cfish.java
it's working fine but when i m adding color as a argument like this:
replace the point.java paint method:
replace the Cfish.java display method:
now it's not working..
how can i pass color as a parameter...??
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18373
|
|
|
When you add a parameter to the paint method, you are no longer overriding it but overloading it instead. You need to keep a reference (instance field) to the color to use.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Punit Jain
Ranch Hand
Joined: Aug 20, 2011
Posts: 702
|
|
|
can you please explain..?
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18373
|
|
|
You know how to create an instance variable of type Color, don't you? Then you change display as you've tried before. paint keeps the same parameter list as originally though (only Graphics), and uses the instance variable in the calls to display.
|
 |
Punit Jain
Ranch Hand
Joined: Aug 20, 2011
Posts: 702
|
|
okay i got it, here is the code:
|
 |
Rob Spoor
Saloon Keeper
Joined: Oct 27, 2005
Posts: 18373
|
|
|
 |
Punit Jain
Ranch Hand
Joined: Aug 20, 2011
Posts: 702
|
|
|
i want to know one more thing in this, how can i make this code into more object oriented form, is it possible?
|
 |
 |
|
|
subject: Problem in passing color as a parameter
|
|
|