• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Head First Java, 2nd Ed: Chapter12 page 364  RSS feed

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

I am only following the book.

davy
 
Ranch Hand
Posts: 2410
  • 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
  • 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
 
Marshal
Posts: 59772
188
  • 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
  • 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
Marshal
Posts: 59772
188
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!