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 8306 articles. 15 followers. Post Follow

9 Replies
434 Views

Similar Articles

[PageSpeed] 27
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:
Similar Artilces:

ZX81 / TS1000 GRAPHIC COMPO!
Bonjour, There is a graphic contest for the best graphic made with a ZX81 or a TS1000 at "http://zx81.repuplika.pl/". If you were a ZX81 or TS1000 user, check this web site and vote! If you still enjoy the ZX81 or TS1000, go and vote! The time limit is September the 30th. Goodby(t)e, ANDRE*** http://www.zx-team.de/andre http://zx81.ordi5.free.fr/andre ...

OSX vs Solaris?
which one is better? * * * Y o u r . S h e p h e r d . A q u i l a . D e u s . ( d 2 0 0 5 x x , d 2 0 0 4 x x , d 2 0 0 3 x x , d 2 0 0 2 x x ) * * * wrote: > which one is better? Who cares? "* * * Y o u r . S h e p h e r d . A q u i l a . D e u s . ( d 2 0 0 5 x x , d 2 0 0 4 x x , d 2 0 0 3 x x , d 2 0 0 2 x x ) * * *" <aquila.deus@gmail.com> > which one is better? > Ford or Chevy... which is better? Puppies or kittens... which are cuter? Taste's Great or Less Filling... which is better? Ralph wrote: > * * * Y o u r ...

Graphics from Printscreen
Hello everyone! This may be a stupid question but.... does procuding a printscreen from a 22" monitor creates a better image than from a 15" monitor? tia Jose Bonifacio wrote: > Hello everyone! > > This may be a stupid question but.... > > does procuding a printscreen from a 22" monitor creates a better image > than from a 15" monitor? Short answer: no. Longer answer: simply swapping monitors does not change the image, but most people would tend to set a 22 inch monitor to a higher resolution than a 15 inch one. Statistically speaking, that would t...

char *arr[n] Vs. char **arr
I need to break a longer string (with strtok()) and store the smaller strings in an array. since the number of small strings is not fixed/known, I cannot use a declaration like char *format[n], so I declared char** format = NULL; Now the problem starts. Each time strtok() returns me a token (which is char*), I call malloc() and I (want to) store the address returned in the array format[], and copy the token to that address. code snippet: ------------------ char **format = NULL; char delim[] = ".-/:"; int i=0; if ((tok = strtok(argv[1], delim)) != NULL) { if((format[...

ANN: Free Vector Graphics ActiveX
http://www.script-debugger.com/ Vector Graphics ActiveX is a graphics component oriented for creating business and technology solutions. Based on COM technology it can be used by other applications to process graphic documents, analyze and visualize any kind of processes. Vector Graphics ActiveX is the ultimate graphics solution for you. It is the easy-to-use for creating technical drawings, illustrations and presentation, training material and business reports, charts and diagrams and much more. Vector Graphics ActiveX offers a wide range of new features and enhancements designed to increase...

Web / Graphic Designer Wahid bin Ahsan
Web / Graphic Designer Wahid bin Ahsan presents Computer Generated Artworks created with Adobe Photoshop and illustrator. http://www.illusionbd.netfirms.com ...

Indy Graphics Options?
Hey all... I'm sitting here with my Indy, and I was wondering... What exactly is in there that makes this little guy chug? I have an XL24 graphics card in there now, and it does 1280x1024x24bit very well. From what I hear, that particular video mode is about 3.75 (4 ish) megabytes in frame buffer, and that most 2d frame buffers are about double because you don't want to draw onto the current frame. (which is a good thing, I can imagine. ;) ) I also have an XL8 sitting in a box in my closet, from before I upgraded the Indy... And from what all I've heard... The XL8 and X...

Qlogic IPS1020 vs ISP1040
I know these are old chips, but I'm dealing with an old system. What are the significant differences between these 2 chips ? Can I expect better throughput with a 1040 ? How much ? Jack ...

Re: Enterprise Guide vs. SAS programmer #5
I have actually used a web service approach whereby a DBA controls the code template and an EG add-in guides a user through the heavy-lift first. That way, the DBA has the main DW code and the user can get their little subset and then play from there. Alan Alan Churchill Savian "Bridging SAS and Microsoft Technologies" www.savian.net -----Original Message----- From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Johnson, David Sent: Tuesday, May 29, 2007 6:16 PM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: Enterprise Guide vs. SAS programmer In assignments (prior to ...

Trouble in exporting graphics
Scilab dies when I try to export from a graphics window (scilab 3.0---linux) I get: Floating Exception /usr/bin/scilab: line 205: 15409 Aborted $SCI/bin/scilex $* When I define the variable PRINTERS, the print command kills scilab the same way. Is this a known issue? Thanks, JPD ...

Q about integrated graphics circuits and dedicated graphics cards
After surfing several hours on web, trying to find out answer, I'm still a bit uncertain about following issue: Are there three types of graphics circuits: 1) Integrated on MB, has an own GPU but no own memory, uses RAM. 2) Integrated on MB, has an own GPU and also own memory, uses this memory and also "borrows" from RAM. 3) Dedicated graphics card with own GPU and memory, uses this memory and also "borrows" from RAM. Earlier, when laptops had only small amount of RAM (like 512 MB), the benefit of dedicated graphics card (later: DGC) with own memory...

How can I generate a graphical file hierarchy
What I really want to do is to show the complexity of existing software, with a view to restructuring it. There are programs out there which will generate a function call tree (I am extremely happy with DoxyGen), but what I want is to generate the relationship between files, not functions. So, if a.cpp calls any function in b.cpp, that is a relationship. I'll accept this for C, but as a bonus for C++, I'd like to see class usage too. Ideally, the result should look like a nice structured tree, but I suspect that some of mine might look like a spider web. Don't know if ascii ar...

Access' dStDev vs Excel's StDev?
I'm assuming that given the same stream of values, each should return the same result. Anybody disagree? -- PeteCresswell On Wed, 30 Mar 2005 20:53:26 -0500, "(Pete Cresswell)" <x@y.z.invalid> wrote: Not me. Just be aware of the difference between StDev and StDevP. But otherwise, yes, StDev is a well defined entity. -Tom. >I'm assuming that given the same stream of values, each should return the same >result. > >Anybody disagree? "(Pete Cresswell)" <x@y.z.invalid> wrote in news:qulm411vve64cn1j7fekt5be6qibov6vde@4ax.com: > Anyb...

[OT] The US Army vs an HP Printer
Mr Brandt - I thought it was funny. Don't be daunted by some of the hypersensitivity= junkies here. * To join/leave the list, search archives, change list settings, * * etc., please visit http://raven.utc.edu/archives/hp3000-l.html * ...

graphic text style setting
Hi, before v6 I set the text style for all the graphics and plots in a notebook with one command at the beginning of the notebook, e.g. $TextStyle[{FontFamily -> "Arial", FontSize -> 16}]; In v6, that does not work anymore. How can I do such a thing in v6? thanks, joerg Hi, BaseStyle ma help: Plot[Sin[x], {x, 0, 2 Pi}, Frame -> True, FrameLabel -> {x, Sin[x]}, PlotLabel -> "A Label", BaseStyle -> {Large, FontFamily -> "Times", Italic}] Regards Jens Joerg Schaber wrote: > Hi, > > before v6...

Boehm GC vs static library (.a)
I'm using the Boehm et al garbage collector; it was not tracing references from static variables that are loaded from a static library. I did a GC_add_roots((char*)&staticvar,(char*)&staticvar+sizeof staticvar); for each library variable and it appears to be acting much nicer. Does this sound like the way the collector is supposed to operate? -- SM Ryan http://www.rawbw.com/~wyrmwif/ But I do believe in this. SM Ryan wrote: > I'm using the Boehm et al garbage collector; it was not > tracing references from static variables that are loaded > from a static library. ...

Update to AppleOldies Website
I have updated the AppleOldies website and added a page dedicated to my graphics converters at the following link. http://www.appleoldies.ca/graphics/index.htm They are not all listed yet, but there's several listed for preparing images for every stock Apple II Graphics Mode including LoRes and Double Lo-Res and of late Super Hi-Res:. For years now, I have wanted to put-up a single page for each type of utilty that I have written for the old computers I write for... I am relatively certain some of these will help someone avoid considerable pain if they can find them wi...

Export vector graphic
Hi, I have the following problem: I want to mark different areas in a plot. therefore I use the fill function. I also want to make those filled areas transparent. This is working fine. But if I set the FaceAlpha to a value smaller than 1 , the pictures is just a bitmap. Even when I export it to eps or pdf the pictures looks really bad after zooming a little bit. example: figure('PaperUnits','centimeters','PaperPositionMode','manual','Paperposition',[0 0 20 12.3609]) plot(1:0.1:10,sin(1:0.1:10)) print -depsc2 bilder/sinus.eps figure('PaperUnits...

LVM vs. Drive Failure
I've done some research about using Logical Volume Management (LVM) to aggregate my hard drives. This is precisely the solution I'm looking for, where I can lump multiple partitions from multiple drives into a single volume resource for Linux (I'm running Ubuntu 6.06). However, several days of reading and searching has not turned up the key issue I'm concerned with. What happens if one of the hard drives in the LVM group kicks the bucket? I know if I had forewarning I could move extents off of that physical device, then remove it from the group and then remove it from the syst...

Graphical Designer
Hi, i wrote an application which helps to draw a room plan. later the plan is used to manage reservations. at the moment, i use inherit classes which can be moved arround. e.g. there are two classes "row" and "table". each class is able to hold serveral "places" which can be arranged with a layoutmanager. now, i have the following problem: each class exports its own configuration jpanel which is displayed when the object is activated. in future i want to have multiple activated object which properties can be changed in one action. i need a machanism...

[LogoForum] Re: converting points to turtle graphics commands
You may find useful some of the procedures in LogoSVG (LogoForum/Files section). Guido Gay LogoForum messages are archived at: http://groups.yahoo.com/group/LogoForum ...

FTP vs.. DSCOPY performance. #8
Hello Paul & @ 3000-l, I will try to sneak this fix into the next 7.5 FTP patch. Regards, James Hofmeister phchristidis@Raytheon.com wrote: HP-3000 Systems Discussion wrote on 01/07/2004 11:02:58 AM: > Hello All @ 3000-l, > > RE: FTP vs.. DSCOPY performance. > << snip >> > Note: At this time 1996 I took over documentation of the FTPDOC. > ARPA.SYS and it currently includes the documentation of all non- > standard (not included in FTP Reference Manuals) features of FTP/iX. > If I missed any let me know. There is a...

Looking for a dual graphic file viewer
One of the techniques I find extremely useful -and yet underexploited- is based on the human eye's remarkable ability to detect imperceptible differences between two consecutive images. Heck, the whole Hollywood and TV industries are based on that simple principle. A Scientific American article argued that such evolutionary trait was caused by the need to detect a rapidly approaching beast, human or otherwise. But I digress... In SCO or Linux machines you can have two screen filled with lots of words and numbers and by quickly alternating the Ctl-Alt-F1 and Ctl-Alt-F2 keys, you can easil...

New Graphics Forum 209609
We have created a new forum for the Artist, Designer & Engineer. We just need users!! Please come on over and have at it! http://www.lasercuttinginc.us/forum Thanks, George www.lasercuttinginc.us "pgeorge777" <pgeorge@lasercuttinginc.us> wrote in message news:54b34295-4123-493b-a12f-8dbb9c3ed383@v3g2000hsc.googlegroups.com... > We have created a new forum for the Artist, Designer & Engineer. We > just need users!! Please come on over and have at it! > http://www.lasercuttinginc.us/forum > > Thanks, > > George > > www.lasercuttinginc...

Reading about Accelerated Graphics
I was wanting some background reading on how VGA, SVGA, 2D accelerated, and 3D accelerated graphics cards work. I'm interested in the technical details, as many as possible, like programming VGA registers and that sort of thing. If anyone knows of any good articles on the web or book that I can buy, please let me know. Jon ...