f



Graphics vs. Graphics2D

I'm working on drawing a gradient.  The Java Developers Almanac just
casts paint()'s Graphics parameter to Graphics2D.

http://javaalmanac.com/egs/java.awt/color_SetGradient.html

How reliable is this?  Will a JPanel's paint (Graphics) method always
be passed a Graphics2D?  A JPanel is 2D, so I guess that would make
sense ...

Any thoughts appreciated.

-- 
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
0
Monique
3/16/2006 5:29:31 PM
comp.lang.java.gui 8282 articles. 0 followers. Post Follow

9 Replies
611 Views

Similar Articles

[PageSpeed] 42

In article <slrne1j83r.it3.spam@home.bounceswoosh.org>,
 "Monique Y. Mudama" <spam@bounceswoosh.org> wrote:

> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
> 
> http://javaalmanac.com/egs/java.awt/color_SetGradient.html
> 
> How reliable is this?  Will a JPanel's paint (Graphics) method always
> be passed a Graphics2D?  A JPanel is 2D, so I guess that would make
> sense ...
> 
> Any thoughts appreciated.

In the Javadocs for Graphics2D, it says that it's the "fundamental class 
for rendering 2-dimensional shapes, text and images" and therefore leads 
me to believe it's reliable.  That article, however, seems to suggest 
overriding the paint method, which is generally not advisable in Swing.  
You should normally override the paintComponent method instead.

= Steve =
-- 
Steve W. Jackson
Montgomery, Alabama
0
Steve
3/16/2006 6:28:16 PM
"Monique Y. Mudama" <spam@bounceswoosh.org> wrote in message 
news:slrne1j83r.it3.spam@home.bounceswoosh.org...
> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
>
> http://javaalmanac.com/egs/java.awt/color_SetGradient.html
>
> How reliable is this?  Will a JPanel's paint (Graphics) method always
> be passed a Graphics2D?  A JPanel is 2D, so I guess that would make
> sense ...
>
> Any thoughts appreciated.

The class hierarchy seems to be as follows:

java.awt.Graphics (abstract)
|- javax.swing.DebugGraphics
|- java.awt.Graphics2D (abstract)
|  |- sun.print.PeekGraphics
|  |- sun.print.ProxyGraphics2D
|  |  `- sun.print.PathGraphics (abstract)
|  |     |- sun.print.PSPathGraphics
|  |     `- sun.awt.windows.WPAthGraphics
|  `- sun.java2d.SunGraphics2D (final)
|- sun.print.ProxyGraphics
|  `- sun.print.ProxyPrintGraphics
`- javax.swing.SystemEventQueueUtilities.RunnableCanvasGraphics (static 
private inner)

    DebugGraphics seem to only come up if you turn on debuging for your 
component via the setDebugGraphicsOptions() method.

    I couldn't find sun.print.ProxyGraphics being used anywhere, and given 
the package name, it's probably for "internal use only".

    RunnableCanvasGraphics has something to do with an optimization for 
applets for situations where it is known before hand that no painting will 
be done, so you should never actually see an instance of this.

    So basically, the only situation you have to worry about is 
DebugGraphics. As long as you never touch the setDebugGraphicsOptions() 
method (and I've never seen a Java program actually use that method), you 
should be okay.

    - Oliver 

0
Oliver
3/16/2006 6:30:21 PM
Monique Y. Mudama wrote:
> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
> 
> http://javaalmanac.com/egs/java.awt/color_SetGradient.html
> 
> How reliable is this?  Will a JPanel's paint (Graphics) method always
> be passed a Graphics2D?  A JPanel is 2D, so I guess that would make
> sense ...
> 
> Any thoughts appreciated.
> 

The Graphics object in a JPanel is always a Graphics2D.  I think but 
can't find it in the docs that all Swing components are the same.

-- 

Knute Johnson
email s/nospam/knute/
0
Knute
3/16/2006 6:47:34 PM
On 2006-03-16, Steve W. Jackson penned:
> In article <slrne1j83r.it3.spam@home.bounceswoosh.org>, "Monique Y.
> Mudama" <spam@bounceswoosh.org> wrote:
>
>> I'm working on drawing a gradient.  The Java Developers Almanac
>> just casts paint()'s Graphics parameter to Graphics2D.
>> 
>> http://javaalmanac.com/egs/java.awt/color_SetGradient.html
>> 
>> How reliable is this?  Will a JPanel's paint (Graphics) method
>> always be passed a Graphics2D?  A JPanel is 2D, so I guess that
>> would make sense ...
>> 
>> Any thoughts appreciated.
>
> In the Javadocs for Graphics2D, it says that it's the "fundamental
> class for rendering 2-dimensional shapes, text and images" and
> therefore leads me to believe it's reliable.  That article, however,
> seems to suggest overriding the paint method, which is generally not
> advisable in Swing.  You should normally override the paintComponent
> method instead.
>

Thanks for the info.  The connection to JPanel was mine; the article
is in the AWT section of the Almanac.  I don't think the article ever
implies you should use it for Swing; rather it just doesn't explicitly
state it's talking about AWT.

-- 
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
0
Monique
3/16/2006 6:52:49 PM
On 2006-03-16, Oliver Wong penned:
>
>     So basically, the only situation you have to worry about is 
> DebugGraphics. As long as you never touch the setDebugGraphicsOptions() 
> method (and I've never seen a Java program actually use that method), you 
> should be okay.
>

Thanks so much.  I hate just flat-out assuming it will always be a
Graphics2D object, but it sounds like I can.  I'll just add a test so
that, if it's ever not a Graphics2D, it will at least draw a single
color.

-- 
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
0
Monique
3/16/2006 7:14:54 PM
Oliver Wong wrote:
> 
>    So basically, the only situation you have to worry about is 
> DebugGraphics. As long as you never touch the setDebugGraphicsOptions() 
> method (and I've never seen a Java program actually use that method), 
> you should be okay.

There's an RFE for DebugGraphics to support Graphics2D. Opened 1999. 
Evaluated as a good idea 2001. 3 votes. You know where the jdk 
collaboration site is if you want something done about it.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262543
https://jdk-collaboration.dev.java.net/

IIRC, some of the PL&Fs cast to Graphics2D without checking, but not 
consistently.

Tom Hawtin
-- 
Unemployed English Java programmer
http://jroller.com/page/tackline/
0
Thomas
3/16/2006 7:28:14 PM
"Thomas Hawtin" <usenet@tackline.plus.com> wrote in message 
news:4419bc11$0$9271$ed2619ec@ptn-nntp-reader01.plus.net...
> Oliver Wong wrote:
>>
>>    So basically, the only situation you have to worry about is 
>> DebugGraphics. As long as you never touch the setDebugGraphicsOptions() 
>> method (and I've never seen a Java program actually use that method), you 
>> should be okay.
>
> There's an RFE for DebugGraphics to support Graphics2D. Opened 1999. 
> Evaluated as a good idea 2001. 3 votes. You know where the jdk 
> collaboration site is if you want something done about it.
>
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4262543
> https://jdk-collaboration.dev.java.net/
>
> IIRC, some of the PL&Fs cast to Graphics2D without checking, but not 
> consistently.

    Thanks for the heads up. I added a vote.

    - Oliver 

0
Oliver
3/16/2006 8:04:34 PM
Monique Y. Mudama wrote:
> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
> 
> http://javaalmanac.com/egs/java.awt/color_SetGradient.html
> 
> How reliable is this? 

Deep down somewhere in some Java documentation (AFAIR it was some 
JDK/JRE release notes), Sun "guaranteed" that with the introduction of 
Java 2D one gets a Graphics2D instead of a Graphics object.

Of course, they screwed it up :-(. At the same time they added 
paintComponent(Graphics). It would have been easy to make that a 
paintComponent(Graphics2D) in Swing, and also add a 
paintComponent(Graphics2D) to AWT.

/Thomas
-- 
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
0
Thomas
3/17/2006 7:59:10 AM
Thomas Weidenfeller wrote:
> 
> Of course, they screwed it up :-(. At the same time they added 
> paintComponent(Graphics). It would have been easy to make that a 
> paintComponent(Graphics2D) in Swing, and also add a 
> paintComponent(Graphics2D) to AWT.

IIRC, Graphics2D was added to 1.2, but Swing was available for 1.1. 
OTOH, you could argue that Swing on 1.2 shouldn't have been compatible 
with Swing for 1.1 (indeed beta versions had different package names).

Tom Hawtin
-- 
Unemployed English Java programmer
http://jroller.com/page/tackline/
0
Thomas
3/17/2006 4:31:58 PM
Reply: