?
Solved

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

Posted on 1999-11-18
7
Medium Priority
?
224 Views
Last Modified: 2010-04-16
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);
          }
      }
0
Comment
Question by:bhuey_ling
  • 4
  • 3
7 Comments
 

Author Comment

by:bhuey_ling
ID: 2217293
Edited text of question.
0
 

Author Comment

by:bhuey_ling
ID: 2217299
urgent!!
0
 
LVL 1

Accepted Solution

by:
Radford earned 300 total points
ID: 2218184
 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:bhuey_ling
ID: 2218787
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
 
LVL 1

Expert Comment

by:Radford
ID: 2220462
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
 

Author Comment

by:bhuey_ling
ID: 2221763
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
 
LVL 1

Expert Comment

by:Radford
ID: 2222489
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Introduction to Processes
Starting up a Project

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question