In Java, how /exactly/ does the "void paint(Graphics)" method work? and what is the Graphics "Context"?

I'm using JPanel in an applet, and have so far figured out that paint overrides the method in JComponent, which JPanel extends. I think somehow JComponent uses a Graphics "Context" but now I'm just way too confused, since I've never heard of a "Context" before. I'm just trying to understand average graphics program a little more in-depth than is introduced to the beginner. So, what exactly happens when I override paint??? Also, what happens when making applets, is the same thing going on??? Please explain, thanks :)

1 Answer

Relevance
  • McFate
    Lv 7
    9 years ago
    Favorite Answer

    Graphics "context" just refers to the instance of Graphics that is properly set up for your specific Component to render itself. You want to draw a line from point A to point B -- but your Graphics (context) knows that point A is offset by the distance of your component from the top-left corner of the Frame that it's in (and at a lower level, that the Frame is offset some distance from the top-left corner of the screen). All of that setup is why you can't just create a Graphics out of thin air, you need the one that the system provides for you.

    If you are interested in a longer discussion of paint, Graphics, and "context", see this FAQ:

    http://www.faqs.org/docs/javap/c6/s3.html

Still have questions? Get your answers by asking now.