Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inside the Graphics class

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inside the Graphics class

    Hello Java Community

    I have a question concerning the inner workings of the graphics class. I know it is not necessary to know anything about that in order to draw stuff. However, I would like to learn more about abstract classes.
    What exactly happens when I pass Graphics g into the paintComponent method?

    So thats what I figured out from the code:

    - JPanel seems to use paintComponent() from JComponent which seems to override that method from its superclass Container (right?)
    - the paintComponent() from JComponent creates a Graphics variable and calls create()
    - I already lost track here.

    What's going on now? ok create() ist called but nothing is really changed. Shouldn't the create method be defined somewhere? How does the compiler know what to do? =)

    I would be glad if someone could explain to me what the compiler does step by step.


    Thanks a lot


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Inside the Graphics class

    What exactly happens when I pass Graphics g into the paintComponent method?
    When you override a method from a super class you need to define its signature exactly as in the super class. The java program somewhere creates a Graphics class object and passes it to the paintComponent() method. Do you have code where you create a Graphics object and call the paintCOmponent() method with it?

    Are you having problems reading the Java SE source code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Inside the Graphics class

    Quote Originally Posted by Garaen View Post
    - JPanel seems to use paintComponent() from JComponent ...
    I imagine that much of the mechanics of paintComponent are to be found in Container, but I haven't looked at the source to know exactly what does what.

    Edit: JPanel doesn't even have a paintComponent method much less a paint method.

    ... JComponent which seems to override that method from its superclass Container (right?)
    No. If you look at the Container API you'll see that it has no such method. The paintComponent method originates in JComponent.


    - the paintComponent() from JComponent creates a Graphics variable and calls create()
    I thought that the Graphics object came directly from the JVM.

    Edit: the Graphics used to paint any UI delegates is created in JComponent's paintComponent method by copying the Graphics object passed in by the JVM, and then disposes this Graphics object once the UI delegates have been painted.

    Have you had a look at one of my favorite articles on Swing graphics?: Painting in AWT and Swing. It's well worth a study.
    Last edited by curmudgeon; September 2nd, 2012 at 09:42 PM.

Similar Threads

  1. Moving content (graphics) inside a JPanel
    By Nesh108 in forum Java Theory & Questions
    Replies: 6
    Last Post: February 1st, 2012, 01:21 PM
  2. Retrieving arrays of java class inside C++ code
    By sattu in forum Java Native Interface
    Replies: 2
    Last Post: May 20th, 2011, 02:15 AM
  3. Graphics class NullPointerException Initialize Graphics Class??
    By bglueck in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 13th, 2011, 11:13 PM
  4. Help with Graphics class and keyPressed method
    By smashX in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 10th, 2011, 12:46 PM
  5. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM