Results 1 to 10 of 10

Thread: Graphics g

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Graphics g

    Hi everyone,

    I have this code:

    Java Code:
    import java.awt.Graphics;
    import javax.swing.JComponent;
    public class HelloWorld extends JComponent 
    {
    	public void paintComponent(Graphics g)
    	{
    		g.drawString("Hello!!!", 110, 110);
    	}
    }
    My question is:

    When I add this component to a frame, I get the text: "Hello!!!". However I am having a hard-time understanding what this Graphics g stand for.

    paintComponent overrides the paintComponentMethod in JComponent,and is called automatically.
    This method gets a parameter of type Graphics. So we have (Graphics g).

    and then I call g.drawString

    But what is this g here?

    Please do not tell me to look to the API. I already have.
    Graphics is an abstract class, so you can not create an object of Graphics right?
    And I nowhere declared a reference g of type Graphics anyway?

    Please help?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Graphics g

    The JVM calls the paintComponent method with a Graphics object. It creates an object of type Graphics and passes a reference to that object to the paintComponent method in your code. You can call methods in the Graphics object to draw on the graphics context that you see in the GUI.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Graphics g

    The JVM calls the paintComponent method with a Graphics object. It creates an object of type Graphics and passes a reference to that object to the paintComponent method in your code.
    I am assuming that "it" reffers to JVM...

    So JVM creates an Graphics object ? and passes the reference g to it.. And we use refference g to call the drawString method.

    Am I right?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Graphics g

    Yes, you use the Graphics object passed to the method to do your drawing.

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Graphics g

    I understand that.

    My question is, who ( or what ) is creating the object ?
    And how come (g) refers to it ?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,653
    Blog Entries
    7
    Rep Power
    21

    Default Re: Graphics g

    Stick a:

    Java Code:
    System.out.printlnI(g.getClass());
    in your paintComponent( ... ) method and see the real class of parameter g. Such an object is created by the AWT framework and your method can use it as if it were a Graphics object.

    kind regards,

    Jos

    ps. note: you'll see quite a bit of identical output, as many lines as your paintComponent( ... ) method is called by the AWT.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Graphics g

    Thank you for your answer.

    So the object belongs to class: sun.java2d.SunGraphics2D.

    For me it is some sort of magic.. Maybe I will undertsand it as I learn more about JAVA.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Graphics g

    For me it is some sort of magic
    Yes, there is lots of "magic" that you have to assume is working correctly. The JVM internals do whatever needs to be done to support the methods.
    Go ahead and use it as the API doc says you can.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,653
    Blog Entries
    7
    Rep Power
    21

    Default Re: Graphics g

    Quote Originally Posted by fatabass View Post
    Thank you for your answer.

    So the object belongs to class: sun.java2d.SunGraphics2D.

    For me it is some sort of magic.. Maybe I will undertsand it as I learn more about JAVA.
    Yup, it's a sub class of the (abstract) Graphics class so you can treat it as such. That class does the real work by calling GL or equivalent graphics methods (depending on the OS it runs on). It is constructed by the AWT and passed to your paintComponent( ... ) method when the AWT wants the component to redraw itself (or parts of it).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Graphics g

    Quote Originally Posted by JosAH View Post
    Yup, it's a sub class of the (abstract) Graphics class so you can treat it as such. That class does the real work by calling GL or equivalent graphics methods (depending on the OS it runs on). It is constructed by the AWT and passed to your paintComponent( ... ) method when the AWT wants the component to redraw itself (or parts of it).

    kind regards,

    Jos
    Many many thanks for the detailed explanation and the time you have spared for it.

    Best Wishes.

    Is there any source that you would recommend for learning Swing?

Similar Threads

  1. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  2. Help with 2d graphics please
    By xbox_nutter in forum New To Java
    Replies: 0
    Last Post: 04-02-2009, 11:48 AM
  3. SWT Graphics Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 06-28-2008, 09:28 PM
  4. Graphics
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-25-2008, 06:24 PM
  5. graphics
    By Joe2003 in forum Advanced Java
    Replies: 4
    Last Post: 01-18-2008, 07:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •