Re: Simple Problem

From: Alex Hunsley (lard_at_tardis.ed.ac.molar.uk)
Date: 04/10/04


Date: Sat, 10 Apr 2004 02:32:49 +0100

Andrew Thompson wrote:

> On Fri, 9 Apr 2004 16:27:20 -0400, Sonia wrote:
>
>
>>Need help with the following problem. This is ex 6.31 from Deitel and
>>Deitel - Java How to Program 4/e [ Not a homework problem , I promise]
>>This is the problem:
>
> ..
>
>>...It
>>says that all the drawing on the applet
>>should be performed by the "paint" method.
>>The problem that I am having is when I add the paint method, it overwrites
>>or overshadows the JTextField that I've created. - it is there but I cannot
>>see it.
>
>
> Are you _sure_ that this book
> is telling you to both.
> a) Use Swing
> b) Override 'paint()'?
>
> That is not right AFAIU, in Swing
> you override paintComponent() instead.
>
> Not that either applets or overriding
> paint/paintComponent are good things
> to be doing at the first stages of
> learning Java.
>

I agree with Andrew here... Sonia, I think perhaps you've misunderstood
the problem. From the sound of the problem, there is no reason at all
the be overriding paint! Maybe you've misunderstood the book? Yes, all
painting is handled by the paint method, which is turn calls things like
paintChildren and paintComponent, but wasn't it just telling you this
for info? I don't think it meant that you were to override paint...
And you can do what you're attempting by not touching paint at all!

(Btw, if you absolutely have to override paint.. the reason it's not
working the way you've done it is that your paint method is empty, and
by empty I mean it doesn't even call super.paint(g).

If you change your paint method to:

  public void paint(Graphics g) {
        super.paint(g);
  } // end of paint method
} //end Ex0631a class

... you'll find that it now paints things correctly. However, if your
paint method looks like this, it's no different to not overriding paint
at all, so don't bother!)

Btw, in your actionPerformed method, you're setting the status bar to
ask the question. Surely you want your applet to be setting the status
to this text much earlier?

If you can be more specific/exact about the question asked by the book,
I can understand things better. For instance, there is no "submit"
button to press once you've typed your answer. Do they expect you to
just hit return after you've typed your answer? And so on....

alex