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 / June 2004

Tip: Looking for answers? Try searching our database.

Pre-draw part of JComponent

Thread view: 
Lars - 28 Jun 2004 11:08 GMT
Hello all,

I have a class Graph that inherits JComponent. It is used for
displaying graphs and consists of a grid and the actual graph "line".

Now, it seems unneccesary to calculate all the coordinates for all the
lines each time the graph is cleared and redrawn (when new values need
to be added to the graph) especially since I have a large number of
graphs that all have exactly the same background grid.

So I would like to draw the grid once and for all and store it so that
it could be shared by all the graphs, and then "copy" it to the
graphics object when I redraw the component.

I'd appreciate any pointers on how to achieve this!

Thanks for your time and help,
/Lars
VisionSet - 28 Jun 2004 11:23 GMT
> Hello all,
>
[quoted text clipped - 11 lines]
>
> I'd appreciate any pointers on how to achieve this!

All drawing must be done from paintComponent.  You cannot separate the grid
from the graphs since that drawing code is used to replace a window that has
popped up over the top of it.  The performance comes from only repainting
what is required through use of the overloaded repaint method that only
repaints a specific rectangle.

repaint(int x, int y, int width, int height)

This is used at a lower level to replace that popped up window.  All you
have to do is do the same for each graph that is drawn.

The calculation will be negligible next to the painting, I'd make sure the
painting is optimised first, then see if you still have a problem.

--
Mike W
Andrew Thompson - 28 Jun 2004 12:52 GMT
> So I would like to draw the grid once and for all and store it so that
> it could be shared by all the graphs,

'BufferdImage'..

<http://groups.google.com/groups?q=g:thl2641509862d&dq=&selm=1dk3u9o29fhkr%24.1ew
tjet4iz3ls.dlg%4040tude.net
>

HTH

Signature

Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Nigel Wade - 29 Jun 2004 10:32 GMT
> Hello all,
>
[quoted text clipped - 14 lines]
> Thanks for your time and help,
> /Lars

Draw the grid into a BufferedImage. Then, in paintComponent draw the image
into the Graphics of the Graph.

This is a potted version of what I do to draw grid lines on a world map.

I use 4BYTE_AGBR to get a transparent image

 gridImage = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR );

then either:
 Graphics gridGraphics = gridImage.getGraphics();
or
 Graphics2D gridGraphics = gridImage.createGraphics();

draw the grid into gridGraphics. I use Graphics2D because I want the
AffineTransforms it allows, but I think you can use Graphics.
If you use Graphics2D dispose() of the gridGraphics if you don't need it
any more.

in the paintComponent of Graph you would, at some point, do:

 g.drawImage( gridImage, 0, 0, this );

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



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.