title: How does the Graphics Class in Java work?
body: <p>Recently, I have been trying to do some Java work with graphics.  Now the Graphics class is abstract, meaning it itself cannot be instantiated but a subclass of it might be able to be instantiated.  So what is going on in the following code. (That does successfully create a line)</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  
  public class line extends JPanel {
      public void paintComponent(Graphics g) {
          g.drawLine(0, 0, 90, 90);
      }
  
      public static void main(String[] args) {
          JFrame jf = new JFrame();
          jf.add(new line());
          jf.setSize(500, 500);
          jf.setVisible(true);
      }
  }
  </code></pre>
  
  <p>Okay now a few things don't make sense to me.  First, when line is called it must enact the paintComponent method in the line class.  That's a property of static methods (being called with the class name).  But the paintComponent method doesn't have a static modifier.</p>
  
  <p>Since this program works though we have to assume that paintComponent does get called and expects a Graphics object as an argument.  When it is called though it has no arguments.  Why doesn't this create an error?  I ran some tests with passing in a String and then nothing into a method that was expecting an Integer object.  I got errors on both programs.</p>
  
  <p>So somehow a graphics object called g is created and the uses the drawline method.  But Graphics is abstract.</p>
  
  <p>Okay so here are my questions:</p>
  
  <ol>
  <li><p>Why is paintComponent being automatically called when it's class line is called when paintComponent isn't static?</p></li>
  <li><p>Why does paintComponent accept empty arguments when it expects a Graphics object?</p></li>
  <li><p>How is the Graphics object g then made as the Graphics class is abstract and cannot be instantiated?</p></li>
  </ol>
  
  <p>Help me Stack Overflow, you're my only hope.</p>
  
  <p>EDIT:
  Okay, I think this question is pretty well answered, thanks!</p>