Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Head First Java, 2nd Ed: Chapter12 page 364

 
Davy Kelly
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Ken,

I am only following the book.

davy
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47274
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic