How to control the color of a Graphic(drawing a shape)

This is part of my program.

What problem of this program?
There is no compile error but when run it, got "java.lang.stackoverflowError"
and at DOS mode got "at Colours2_5$DisplayCanvas.setColor(compile code)

Can't i write "setColor"? How to solve it if i want control the color of the oval?"

class DisplayCanvas extends Canvas
      {
        public DisplayCanvas()
        {
          setColor(Color.red);
        }
      
        public void setColor(Color colour)
        {
          setColor(colour);
          repaint();
        }                                          
        public void paint(Graphics g)
        {
          g.fillOval(20,20,100,150);
          g.fillOval(120,20,160,150);
          g.setFont(new Font("Helvetica", Font.BOLD,12));
          g.drawString("LED",39,160);
          g.drawString("Fiber-optic",134,160);
          }
      }
bhuey_lingAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bhuey_lingAuthor Commented:
Edited text of question.
0
bhuey_lingAuthor Commented:
urgent!!
0
RadfordCommented:
 public void setColor(Color colour)
  {
    setColor(colour);
    repaint();
  }                                            
  This method is recursive and it never stops calling itself.  Inside the method, instead of calling setColor(colour), you should call either setForeground(colour) or setBackground(colour)

Those two methods are in the Component class which the Canvas class subclasses from.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

bhuey_lingAuthor Commented:
Dear sir,
thanks for reply.

However, I still face the same problem after i change the "setColor" to "setForeground".

I face same error and this method still is recursive.

No compile error but run time error. Still is java.lang.stackoverflowError and at DOS mode, got "at Colours2_5$DisplayCanvas.setForeground(compile code)

Why?

Below is the part of program that i had been change.

class DisplayCanvas extends Canvas
   {
     public DisplayCanvas()
     {
      setForeground(Color.red);
     }
      
     public void setForeground(Color colour)
     {
      setForeground(colour);
      repaint();
      }                                                                              public void paint(Graphics g)
      {
       g.fillOval(20,20,100,150);
       g.fillOval(120,20,100,150);
       g.setFont(new Font  ("Helvetica", Font.BOLD,12));
       g.drawString("LED",39,160);
       g.drawString("Fiber-optic",134,160);
       }
}

Thanks for reply my question.
0
RadfordCommented:
No no no, you shouldn't have changed the NAME of your method.  This is what you should do:


class DisplayCanvas extends Canvas
   {
     public DisplayCanvas()
     {
      setColor(Color.red);
     }

     public void setColor(Color colour)
     {
       setForeground(colour);
       repaint();
     
     }

...
...
...
the rest of your code
...
...
...
0
bhuey_lingAuthor Commented:
thanks and it really work !!
however, may i ask again:

At first, i would like to call the method but i write it wrongly to

a.setForeground(Color.red);

why it can run too? but after i change to method name

a.setColor

it still can run but not as smooth as previous, why?

thnx!!!!..*^^*
0
RadfordCommented:
I'm not sure, but if the only thing that your setColor method does is call setForeground, then what's the point of having that method at all?  Just call setForeground in the constructor.  I don't even think you need to call repaint() manually.  It should be called automatically already.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.