Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in passing color as a parameter

 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain..?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay i got it, here is the code:



 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Punit Jain
Ranch Hand
Posts: 1012
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to know one more thing in this, how can i make this code into more object oriented form, is it possible?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic