Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

createGraphics vs getGraphics methods

0 views
Skip to first unread message

Randall Parker

unread,
Feb 14, 2001, 6:17:47 PM2/14/01
to
I'm looking in Java 1.2. So perhaps Java 1.3 has changed what I'm about
to describe...

java.awt.image.BufferedImage has both the old style method getGraphics()
that returns a java.awt.Graphics object (which can then be cast to
java.awt.Graphics2D) and createGraphics() which returns a
java.awt.Graphics2D directly. Okay, that's nice that they added
createGraphics().

But over in java.awt.Component and javax.swing.JComponent there is the
familiar getGraphics() but no createGraphics().

Anyone know why Sun added createGraphics() in one place and not in other
obvious places where it would also seem to be useful?

Also, do all calls to getGraphics(), regardless of which class supports
it, always return a java.awt.Graphics object that can be cast to
java.awt.Graphics2D? ie are all java.awt.Graphics objects produced by the
runtime lib now all really java.awt.Graphics2D objects? Any place where
that is not the case?

Kevin Weiner

unread,
Feb 15, 2001, 12:51:53 AM2/15/01
to

Randall Parker wrote:
>
> java.awt.image.BufferedImage has both the old style method getGraphics()
> that returns a java.awt.Graphics object (which can then be cast to
> java.awt.Graphics2D) and createGraphics() which returns a
> java.awt.Graphics2D directly. Okay, that's nice that they added
> createGraphics().
>
> But over in java.awt.Component and javax.swing.JComponent there is the
> familiar getGraphics() but no createGraphics().
>
> Anyone know why Sun added createGraphics() in one place and not in other
> obvious places where it would also seem to be useful?

BufferedImage.createGraphics() generates a brand new graphics context
based on the image as the drawing "device". It's a different situation
with Component and friends, and their getGraphics() just returns the
existing context that was last used to paint the component.

-- Kevin

Linda Radecke

unread,
Feb 15, 2001, 10:19:54 AM2/15/01
to

Randall Parker escribió:

> I'm looking in Java 1.2. So perhaps Java 1.3 has changed what I'm about
> to describe...
>
> java.awt.image.BufferedImage has both the old style method getGraphics()
> that returns a java.awt.Graphics object (which can then be cast to
> java.awt.Graphics2D) and createGraphics() which returns a
> java.awt.Graphics2D directly. Okay, that's nice that they added
> createGraphics().

Because of BuffferedImage is a subclass of Image it has the
getGraphics-method,
but it's deprecated, you'd better use createGraphics() instead.

> But over in java.awt.Component and javax.swing.JComponent there is the
> familiar getGraphics() but no createGraphics().

CreateGraphics is only used for BufferedImages.

>Anyone know why Sun added createGraphics() in one place and not in other
> obvious places where it would also seem to be useful?

> Also, do all calls to getGraphics(), regardless of which class supports
> it, always return a java.awt.Graphics object that can be cast to
> java.awt.Graphics2D?

AFAIK no, only the createGraphics()-method can be used for Graphics 2D.

Linda

Linda Radecke

unread,
Feb 15, 2001, 10:22:57 AM2/15/01
to

Randall Parker escribió:

> I'm looking in Java 1.2. So perhaps Java 1.3 has changed what I'm about
> to describe...
>
> java.awt.image.BufferedImage has both the old style method getGraphics()
> that returns a java.awt.Graphics object (which can then be cast to
> java.awt.Graphics2D) and createGraphics() which returns a
> java.awt.Graphics2D directly. Okay, that's nice that they added
> createGraphics().

Because of BuffferedImage is a subclass of Image it has the


getGraphics-method, but it's deprecated, you'd better use
createGraphics() instead.

>But over in java.awt.Component and javax.swing.JComponent there is the


>familiar getGraphics() but no createGraphics().

createGraphics() is only used for BufferedImages.

>Anyone know why Sun added createGraphics() in one place and not in other
>obvious places where it would also seem to be useful?
>Also, do all calls to getGraphics(), regardless of which class supports
>it, always return a java.awt.Graphics object that can be cast to
>java.awt.Graphics2D?

AFAIK no, only the createGraphics()-method can be used for Graphics 2D.

Linda

0 new messages