#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Understanding how the Graphics class works


    Learning graphics in Java, and this is confusing me a lot.


    Code:
    public void paint(Graphics g)
    {
    
    super.paint(g);
    }
    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.

    Also, what does super.paint() do, and is it only coincidence that the calling method is also named paint?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by bullet
    You're right, Graphics is an abstract class.

    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.
    I thought it was impossible to instantiate an abstract class.

    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?
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by Rukifelth
    I thought it was impossible to instantiate an abstract class.

    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?
    You cannot instantiate an abstract class using the new operator.

    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.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    To see the name of the real class of the g object, print out the value returned by getClass:
    Code:
    System.out.println("g class="+g.getClass());

IMN logo majestic logo threadwatch logo seochat tools logo