Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2006

Tip: Looking for answers? Try searching our database.

Graphics vs. Graphics2D

Thread view: 
Monique Y. Mudama - 16 Mar 2006 18:26 GMT
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.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Steve W. Jackson - 16 Mar 2006 19:28 GMT
> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
[quoted text clipped - 6 lines]
>
> 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 =
Signature

Steve W. Jackson
Montgomery, Alabama

Monique Y. Mudama - 16 Mar 2006 19:50 GMT
>> I'm working on drawing a gradient.  The Java Developers Almanac
>> just casts paint()'s Graphics parameter to Graphics2D.
[quoted text clipped - 13 lines]
> 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.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Oliver Wong - 16 Mar 2006 19:30 GMT
> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
[quoted text clipped - 6 lines]
>
> Any thoughts appreciated.

The class hierarchy seems to be as follows:

java.awt.Graphics (abstract)
|- javax.swing.DebugGraphics
|- java.awt.Graphics2D (abstract)
[quoted text clipped - 6 lines]
|- 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
Monique Y. Mudama - 16 Mar 2006 20:12 GMT
>     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.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Thomas Hawtin - 16 Mar 2006 20:28 GMT
>    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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Oliver Wong - 16 Mar 2006 21:04 GMT
>>    So basically, the only situation you have to worry about is
>> DebugGraphics. As long as you never touch the setDebugGraphicsOptions()
[quoted text clipped - 10 lines]
> IIRC, some of the PL&Fs cast to Graphics2D without checking, but not
> consistently.

   Thanks for the heads up. I added a vote.

   - Oliver
Knute Johnson - 16 Mar 2006 19:47 GMT
> I'm working on drawing a gradient.  The Java Developers Almanac just
> casts paint()'s Graphics parameter to Graphics2D.
[quoted text clipped - 6 lines]
>
> 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.

Signature

Knute Johnson
email s/nospam/knute/

Thomas Weidenfeller - 17 Mar 2006 08:59 GMT
> 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
Signature

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/

Thomas Hawtin - 17 Mar 2006 17:31 GMT
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.