Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Graphics Object & Graphics Context  RSS feed

Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Pals,
What is the difference between a Graphics context and a Graphics object ?
Graphics class is an Abstract class -- then how can we have a Graphics object ?
In the paint method public void paint(Graphics g) :- what is exactly passed does it expect a reference to an object of an abstract class ?
All methods of Graphics class are abstract e.g drawString(), drawRect() etc...they are not implemented in the Graphics class
Then where or in which subclass are these methods implemented ?
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Graphics is an abstract class, but your paint method can work with a reference to it due to polymorphism. When you invoke a method that takes (Graphics g) as an argument, what is passed to the method is a reference to your particular graphics context, which extends the Graphics class. By creating the paint method to take the abstract class, you ensure that you can use the method the same way, regardless of particular context.
Eric B.
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks pal,
But still its not clear :
1) Which is the concrete class that implements all the methods
of the abstract Graphics class.
2) The Graphics class though being abstract has a protected
Constructor. There is nothing inside the constuctor.(No
coding). What is the use of that such a protected constructor
that too for an abstract class which does not allow you to
create an object of itself ?
3) I just want to know who Implements all the abstract methods
of the Graphics class. Who calls the protected constructor
of this class.
- Happy Java Programming
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The abstract methods of the Graphics class are overridden
in its direct subclass DebugGraphics.
Re the constructor.....from the API.......
Since Graphics is an abstract class, applications cannot
call this constructor directly. Graphics contexts are
obtained from other graphics contexts or are created by
calling getGraphics on a component."
Hope this helps.
- satya

Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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..
maha anna

[This message has been edited by maha anna (edited June 04, 2000).]
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maha anna
Thanks for the great explanation of Graphics! It really helped me to understand how Graphics works in Java.
Thanks again
ARS Kumar.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!