Vijay,
As you said the Graphics class is an abstract class. Basically an object of this Graphics class
represents the context using which the drawing is done by the AWT
thread. These 2 words
Graphics and
Graphics context are inter-linked. They are related to each other. In other words , a physical Graphics object has many attributes used for drawing the component. The WHOLE set of attributes which are
1. which component to draw
2. which area of this component to draw
3. what is the color used to draw
4. what is the Font type used if there is a need to write a char
etc.
I always imagine the whole sequence of operations used during drawing , as though you as a person given a piece of paper and a whole set of different pens filled with different coloured inks. You as a person what will do ?
You 1. spread the paper
2. pickup up your favorate color pen
3. decide what you are going to draw, and on which area of the piece of paper you are going to use
4. What's the
style you are going to use for the writing of chars (especially for your signature
)
What happens in our ordinary life only happens in Java Drawing. I think now you can relate to the 2 words you mentioned as
Graphics context and the
Graphics object itself. All the 4 attributes constitute the context. The no-more abstract(the JVM created Graphics object) has the
methods for drawing a set of predefined skills of drawing/filling a line/rectangle/polygen/oval etc. In other words you as an artist has the
talent of drawing various pictures, and act as the physical Graphics object.
As you know apart from all the user threads which you yourself create in your program, there are many daemon (service ) threads which run in the background when the JVM is loaded into memory. The GC (garbage collection) thread,the AWT drawing thread are of this nature. The
JVM creates the Graphics object , the AWT daemon thread uses this Graphics object and calls the paint(Graphics g) or update(Graphics g) method of any component which has to be drawn.
When will the drawing operation take place ? The
Head of the family (JVM) knows it all.
It knows
1.
when to draw (the com is first shown OR hidden and then shown again OR when you call repaint() of the comp etc.),
2.
which component to draw, (generally drawing takes place from top to bottom component hirearchy. In other words a parent(container) draws itself first before any of the contained components are drawn. If there is a Frame which has again a panel, the panel has a button and a text box, when you make frameRef.setVisible(true), the frame draws itself, then the panel draws itself, then the button and textbox draw themself. The order of textbox and Button drawing are not fixed. Since they are at the SAME level. BUt I think if a bottom level comp needs to be repaired, (i.e) when you call repaint() of a comp, AWT does not waste time in redrawing ALL the ABOVE parents. It just repairs the needed portion of the component alone.).
3.
which area of the comp alone needs to be repaired (at the time of only part of a comp is shown),
4.
what font 5.
what color etc.)
Basically the
chief (in real world who is the
cheif ?
You know its who.
) creates the Graphics object (the artist) and calls the paint(..) method. What you do with the Graphics object (with the artist) depends on what is
inside the paint method. A component (Button/Checkbox/Choice..) override the inherited paint(..) method from Component class and choose their own way of drawing. So only each one looks differently even though basically all are a
rectangle component.
The Component's
Graphics getGraphics() method is just to get the
used Graphicis Context (the Graphics object which has the 4 attributes of the context), which WAS USED by the AWT thread for the ALREADY DRAWN/SHOWN component. Also note that when a component is shown in the screen only you can call the getGraphics() method. If the comp is not shown in the screen, this method just will return null.
It is asthough after you finishup a drawing when somebody asks about the
description of your picture. Not so exactly how you describe. You just write in a piece of paper or put a note on the picture.. But this getGraphics gives another Graphics object itself. Who is this another Graphics object for you. It is upto your imagination..
regds
maha anna
[This message has been edited by maha anna (edited June 04, 2000).]