Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

big confusion related to Graphics class  RSS feed

 
vibhor malik
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am learning java from an online reference..and I am stuck on GUI chapter...
there is a code to write applet or stand alone program,doesn't matter...




Here...
We are importing java.awt.*; and hence Graphics class is automatically imported...
online reference material says that to draw something we have to use Graphics class's object and its methods...ex g.drawRect, g.fillColor etc
Here, in first step we extends JPanel and over-ride paintComponent method and in this method we are taking an object of type Graphics in the parameters..
which means that when any other class will call paintComponent method of this code then first it has to first make Graphics object and then pass it in actual arguments..
but here nothing like this is happening...(If this question is not understood), then lets see from another angle)

ok, now
If we see it from other way..
in the next line of code we are calling g.drawString method...but how can we do it...because only a reference of Graphics type is made(in the parameters, (Graphics g)) but no real object is made yet...then how can we call this method without no real object in memory...

Please I am really confused here...

And please elaborate your explanation if possible..
I Know I am asking a lot...but this is the foundation of my knowledge...

Thanks
Vibhor Malik
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't call the line g.drawString unless you are in your paintComponent method. In order to even get into that method you have to pass it a Graphics object, which is g.
 
Campbell Ritchie
Sheriff
Posts: 53764
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please use the code button; since you are new I shall edit your post and you will see how much better it looks.

I shall move this thread to another forum where we usually discuss GUI matters.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vibhor malik wrote:online reference material says that to draw something we have to use Graphics class's object and its methods...ex g.drawRect, g.fillColor etc
Here, in first step we extends JPanel and over-ride paintComponent method and in this method we are taking an object of type Graphics in the parameters..
which means that when any other class will call paintComponent method of this code then first it has to first make Graphics object and then pass it in actual arguments..

Not in client code. You call repaint() and leave it to the repaint manager to schedule a repainting with an appropriate Graphics object.

in the next line of code we are calling g.drawString method...but how can we do it...because only a reference of Graphics type is made(in the parameters, (Graphics g)) but no real object is made yet...then how can we call this method without no real object in memory...

The Graphics reference passed to the method is indeed a "real object in memory" as you understand it.

More conventionally, it is non-null. If you really want to know where it originates, step through the code with a debugger ... and even then you won't be able to step into native methods.

But really, you needn't worry your head over it. More here:
Painting in AWT and Swing
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!