Forums Register Login

Head First Java, 2nd Ed: Chapter12 page 364

+Pie Number of slices to send: Send
Hey everyone,

I have not been here in a while, but I was going through the Head First Java 2nd Edition book, and I must say I have really enjoyed it.

it has been a lot more easy to understand than the course I done, which was that hard, I took time out from programming, but I want to get back into it.

I have got up to Chapter 12 page 364 with no probs at all, in fact it has been refreshing and I remember most of it, some changes from 1.3 to 1.5

Anyway the part I am stuck with is the code

and any subsequent code on pages 365/6/7

it does not run (i thought it would not) because there is no main method.

but if I was to call the paintComponent method in a main method, how do I get a method with the argument Graphics?

I have racked my brains, and got scaley eyes reading the API

Please help, as I would like to understand what is going on.

Davy
+Pie Number of slices to send: Send
That's not how you would go about constructing and displaying a Swing GUI. You would create a JFrame and add the panel to the JFrame then call setVisible(true) on that JFrame.
+Pie Number of slices to send: Send
hey Ken,

I am only following the book.

davy
+Pie Number of slices to send: Send
Davy,
I'm looking at the book, and basically they are telling you in those two paragraphs on page 364 what Ken said. You create a JFrame and add the JPanel to it. When the frame is displayed, the panel's paintComponent will be called. You can't create a Graphics object yourself.
+Pie Number of slices to send: Send
but I am still none the wiser as to how to get this to work!

and how do i put the graphics argument in? I mean what am I using?
I can't call this method without the argument and that argument should be the same object.

davy
+Pie Number of slices to send: Send
 

how do i put the graphics argument in?

I don't know. I have never worked out how to put the Graphics argument in. But I just writeand it works.

I can only presume there is some object in the background which sends Graphics objects to the paintComponent method whenever they are needed.
+Pie Number of slices to send: Send
campbell,

I still need to call this method, if I have to put it onto a frame. so the super.paintComponent(g) should not help, cause it is inside the method I am calling.

davy

EDIT: I got it, I don't have to call the method, I just add an object of the class onto the frame.

this is the code for it to work, thanks for helping me see the errors of my ways
[ June 20, 2006: Message edited by: Davy Kelly ]
+Pie Number of slices to send: Send
It's nice when it works, isn't it.
Glad to be able to help
That new kid is a freak. Show him this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1759 times.
Similar Threads
java.lang.NoSuchMethodError ??
Resize JFrame to fit image
not able to get the coloured oval
Creating own grpahic widget
need help using the paintComponent method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:56:37.