Re: Simple Problem - Question from Book

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


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

Sonia wrote:

> "Alex Hunsley" <lard@tardis.ed.ac.molar.uk> wrote in message
> news:b8Idc.53063$Id.33604@news-binary.blueyonder.co.uk...
>
>>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
>>
>>
>
>
> Guys,
> Here is the extact question from the book:
> 6.31) Computers are playing an increasing role in education. Write a program
> that will help an elementary school student learn multiplication. Use
> Math.random to produce two positive one-digit integers. The program should
> then display a question in the status bar, such as
>
> How much is 6 times 7?
>
>
> The student then types the answer into a JTextField. Next, the program
> checks the student’s answer. If it is correct, draw the string “Very good!”
> on the applet and ask another multiplication question. If the answer is
> wrong, draw the string “No. Please try again.” on the applet and let the
> student try the same question again repeatedly until the student finally
> gets it right. A separate method should be used to generate each new
> question. This method should be called once when the applet begins execution
> and each time the user answers the question correctly. All drawing on the
> applet should be performed by the paint method.
>
>
> I know how to solve the problem without the pain method, but the question
> askes for it, so i;ve tried but failed to do so. If you can give me
> pointers that would be great. Then I will worry about the rest of the
> program.
>
> Thanks Guys

Hi Sonia
I have no experience with this books, but it sounds very bad! The fact
that it says "All drawing on the applet should be performed by the paint
method" at the end is completely irrelevant (although true) and
misleading. Also I notice it gives you no hint as to maybe using a
button to submit the answer; and I must say that the status line is
*not* a good place for normal user interaction to take place; that
should happen in the applet itself.

I would find a different book, as this one sounds like it's been written
by someone who doesn't know what they're talking about!

Try "Thinking in java" perhaps? It's free to download and it's very good:

http://mindview.net/Books/TIJ/DownloadSites

alex


Quantcast