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 6 of 6

Thread: getGraphics()

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    66
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default getGraphics()

    What is the difference between getGraphics() and getComponentGraphics(Graphics g). I suppose then the question really is what is the, "original," graphics object?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,390
    My Mood
    Mellow
    Thanks
    83
    Thanked 471 Times in 462 Posts

    Default Re: getGraphics()

    Provide an API reference for getComponentGraphics(Graphics g).

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    KAJLogic (October 21st, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    66
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: getGraphics()

    Apologies I have work and school to juggle so it may take some time for my reply thanks for waiting.

    getGraphics()

    Returns this component's graphics context, which lets you draw on a component. Use this method to get a Graphics object and then invoke operations on that object to draw on the component.

    getComponentGraphics()

    Returns the graphics object used to paint this component. If DebugGraphics is turned on we create a new DebugGraphics object if necessary. Otherwise we just configure the specified graphics object's foreground and font.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,390
    My Mood
    Mellow
    Thanks
    83
    Thanked 471 Times in 462 Posts

    Default Re: getGraphics()

    No worries.

    You went above and beyond with the last post. I was just looking for a link to getComponentGraphics() API, because I couldn't find it. I was having a bad day, I guess, because there it is right on the JComponent page where it should have been. The reason I asked for the link was to gather the specifics of the two calls that you didn't include originally or even in your latest post.

    For example, notice both return a Graphics object; that getGraphics() doesn't accept a parameter, so it's use would be:

    Graphics g2 = component.getGraphics();

    and that getComponentGraphics( Graphics g ) DOES accept a parameter.

    From there, it seems they're essentially the same, EXCEPT when the debug graphics options are set, the getComponentGraphics() method returns a DebugGraphics object instead of a normal Graphics object. I've never used a DebugGraphics object, so I'm not sure what you'd do with one if you had it. In "Java Swing" (Loy, et. al.) it says that when the DebugGraphics object is used, drawing is slowed or logged to help the user identify problems. I'll have to try that someday.

    Thanks for asking.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    KAJLogic (October 21st, 2013)

  7. #5
    Member
    Join Date
    Sep 2013
    Posts
    66
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: getGraphics()

    This clears it up perfectly. I will try to improve my posting etiquette.

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,390
    My Mood
    Mellow
    Thanks
    83
    Thanked 471 Times in 462 Posts

    Default Re: getGraphics()

    While we should all strive to do better at all we do, I don't think you did anything wrong. Forgive me if I gave that impression.

Similar Threads

  1. odd message from Component.getGraphics()
    By Melawe in forum Java Theory & Questions
    Replies: 8
    Last Post: May 28th, 2012, 08:17 AM