wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Problem in passing color as a parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem in passing color as a parameter" Watch "Problem in passing color as a parameter" New topic
Author

Problem in passing color as a parameter

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
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

Joined: Oct 27, 2005
Posts: 19655
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
can you please explain..?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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: 980
    
    2
okay i got it, here is the code:



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 980
    
    2
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
 
Similar Threads
Bouncing balls
Arrow key commands and paint method in canvas (mac vs pc)
Using Threads
Grid Alignment and Null Exception
Applet/ Animation/ repaint-update-paint ?