File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Graphics g Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Graphics g" Watch "Graphics g" New topic

Graphics g

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Hello. I'm looking through several examples of uses of the Graphics class and they all seem to follow this kind of format :

public void paint(Graphics g)
My question is, how do I call this type of method? I'm trying these classes myself but I get stuck everytime I try to slot the paint method into my class. It looks like I need to pass a Graphics object when I call the method but I cannot instantiate a Graphics class because it's abstract.
Thanks for any help!
Jesper Ottosson
Ranch Hand

Joined: Mar 21, 2000
Posts: 51
you can't call the paint method directly, instead you must go through the repaint() method of your component. AWT (or swing) will then create the proper graphics object, and send it to your paint method.

Joined: Feb 28, 2000
Posts: 3
As Jesper noted, the method to call is repaint which calls update which eventually calls paint. The manner in which the JVM provides the Graphics reference is not all clear to me, and then there is the thing called a 'graphics context'. Well, I suspect the graphics context is just a way of saying that (my words) 'the paint method being passed a reference of type graphics, provides a context in which to direct the graphical display of a component (of which method paint is a member). This looks like a good example of delegation. The component delegates all of its graphical manipulation to some hidden object accessed through the reference passed to the method void paint(Graphics g).
The reference of type Graphics allows access to all of the abstract methods in the Graphics class. (Java2 API doc is worth a peak). The methods manage the graphical state of the component.
To use the Graphics methods, place calls to them from a components' paint method, but you don't call paint directly. As Jesper noted, invoke the method repaint and it will in turn call update which eventually calls paint and runs the Graphics methods invoked within paint's body.
I agree. Here's the link:
subject: Graphics g
It's not a secret anymore!