Results 1 to 10 of 10
Thread: Graphics g
- 12-24-2011, 06:46 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Graphics g
Hi everyone,
I have this code:
My question is: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); } }
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?
- 12-24-2011, 06:52 PM #2
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.
- 12-24-2011, 06:55 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Graphics g
I am assuming that "it" reffers to JVM...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.
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?
- 12-24-2011, 07:02 PM #4
Re: Graphics g
Yes, you use the Graphics object passed to the method to do your drawing.
- 12-24-2011, 07:04 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Graphics g
I understand that.
My question is, who ( or what ) is creating the object ?
And how come (g) refers to it ?
- 12-24-2011, 08:04 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: Graphics g
Stick a:
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.Java Code:System.out.printlnI(g.getClass());
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.When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-24-2011, 08:10 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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.
- 12-24-2011, 08:12 PM #8
Re: Graphics g
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.For me it is some sort of magic
Go ahead and use it as the API doc says you can.
- 12-24-2011, 08:43 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,380
- Blog Entries
- 7
- Rep Power
- 17
Re: Graphics g
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-25-2011, 12:22 AM #10
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
Drawing a graphics onto another Graphics ?
By Ziden in forum Java AppletsReplies: 0Last Post: 01-08-2011, 07:30 PM -
Help with 2d graphics please
By xbox_nutter in forum New To JavaReplies: 0Last Post: 04-02-2009, 11:48 AM -
SWT Graphics Example
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:28 PM -
Graphics
By Joe2003 in forum Advanced JavaReplies: 1Last Post: 01-25-2008, 06:24 PM -
graphics
By Joe2003 in forum Advanced JavaReplies: 4Last Post: 01-18-2008, 07:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks