aspose file tools*
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 928
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: 19541
    
  16

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: 928
can you please explain..?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

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



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 928
i want to know one more thing in this, how can i make this code into more object oriented form, is it possible?
 
Consider Paul's rocket mass heater.
 
subject: Problem in passing color as a parameter
 
Similar Threads
Grid Alignment and Null Exception
Arrow key commands and paint method in canvas (mac vs pc)
Using Threads
Bouncing balls
Applet/ Animation/ repaint-update-paint ?