October 16th, 2013, 10:40 PM
Understanding how the Graphics class works
Learning graphics in Java, and this is confusing me a lot.
This confuses me because I thought Graphics was an abstract class, and yet here I appear to be passing an argument from that class to a superclass paint method.
public void paint(Graphics g)
Also, what does super.paint() do, and is it only coincidence that the calling method is also named paint?
October 16th, 2013, 11:14 PM
You're right, Graphics is an abstract class.
When an instance of your class becomes visible for the first time or is asked to repaint itself, a Graphics object is generated (not by the programmer) and sent to the paint method.
You send that Graphics parameter to the superclass's paint method to make that everything is drawn correctly.
October 17th, 2013, 11:33 AM
I thought it was impossible to instantiate an abstract class.
Originally Posted by bullet
So if i'm understanding this, super.paint(g) passes a new graphics object to the superclass's paint method (the superclass being JApplet), and then any further method calls (like g.setBackground()) propagate themselves back down into the subclasses?
October 17th, 2013, 11:56 AM
You cannot instantiate an abstract class using the new operator.
Originally Posted by Rukifelth
Typically what happens is you create a concrete subclass of the abstract class that fills in the missing pieces. You create an instance of that.
super.paint(g) sends the Graphics object that was sent as parameter up to the superclass's paint method.
You can see the effects sometimes of not doing this. You can add a button or combo box, and it you don't include the call to the superclass's paint method, then you would have to move the mouse over the button or combo box for it to show up.
October 17th, 2013, 12:49 PM
To see the name of the real class of the g object, print out the value returned by getClass: