jQuery in Action, 3rd edition
The moose likes Swing / AWT / SWT and the fly likes Swing Components and Graphics Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing Components and Graphics Objects" Watch "Swing Components and Graphics Objects" New topic

Swing Components and Graphics Objects

Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
I'm trying to learn a little about Swing before I make my first attempt at programming a GUI. I had a couple of questions:
When is a graphics object passed to a Swing Component for painting?
Does this only happen with custom components, or all components?
What does the graphics object help the compnent do?
How is it involved in the painting process?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
A Graphics object is passed to the paintComponent() method of any component that needs to be painted. AFAIK, this happens with all components. The Graphics class provides functions that allow you to paint on a window or other component. These methods include drawLine(), drawRect(), and many others. For more details, you should look at the API documentation for the Graphics class. It lists all the methods that are available for your use. In fact, you should bookmark the index for the API docs for the version of the Java SDK that you are using and learn how to navigate it. These documents provide most of the information you need about any class in the API.

Java API Documentation
The Java Tutorial
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
Moving this to the Swing / JFC / AWT forum...

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Landon take a look at The Java Tutorial

SCJP2. Please Indent your code using UBB Code
Landon Blake
Ranch Hand

Joined: Dec 04, 2003
Posts: 121
Thanks for the responses.
I have taken a look at the API for the graphics class, and also the tutorial.
I understand that you pass a graphics object to a component when it is painted.
Do I create this graphics object? How do I identify the appropriate graphics object to pass as an argument if I have more than one component?
(Doesn't each component have its own graphics object?)
Thanks again for any info or suggestions.
[ March 01, 2004: Message edited by: Landon Blake ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No you do not need to create them. Read more in Introduction to painting concepts
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
For the majority of Swing guis you do need to worry about the Graphics object(s). The Swing components take care of this for you.
I agree. Here's the link: http://aspose.com/file-tools
subject: Swing Components and Graphics Objects
It's not a secret anymore!