Reputation: 71
Why paint method requires graphics objects as an argument? Passing graphics get as an argument here.
public void paint(Graphics g)
Upvotes: 1
Views: 515
Reputation: 726589
paint
method requires Graphics
as an argument because otherwise it would not know in what graphic context the drawing needs to be made.
An implementation of paint
needs to perform its drawing in some context, i.e. make calls like g.drawImage
etc. Without a Graphics
parameter the paint
would have to have some alternative way of getting hold of that g
parameter, e.g. though some static method or a variable. This approach would be less explicit than providing the context as a method parameter.
Letting the caller decide what Graphics
to pass to your paint
method has an added benefit: the content of your applet can be captured for printing without any additional work on your part. The host of your applet simply passes a printer's graphic context to the same print
method to get a printable image of your running applet.
Upvotes: 1