jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Head First Java, 2nd Ed: Chapter12 page 364 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java, 2nd Ed: Chapter12 page 364" Watch "Head First Java, 2nd Ed: Chapter12 page 364" New topic
Author

Head First Java, 2nd Ed: Chapter12 page 364

Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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


How simple does it have to be???
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
hey Ken,

I am only following the book.

davy
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
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.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
It's nice when it works, isn't it.
Glad to be able to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java, 2nd Ed: Chapter12 page 364
 
Similar Threads
Resize JFrame to fit image
Creating own grpahic widget
need help using the paintComponent method
java.lang.NoSuchMethodError ??
not able to get the coloured oval