 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Lars Guest
|
Posted: Mon Jun 28, 2004 10:08 am Post subject: Pre-draw part of JComponent |
|
|
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
|
|
| Back to top |
|
 |
VisionSet Guest
|
Posted: Mon Jun 28, 2004 10:23 am Post subject: Re: Pre-draw part of JComponent |
|
|
"Lars" <larsgj (AT) hotmail (DOT) com> wrote
| Quote: | 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!
|
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
|
|
| Back to top |
|
 |
Andrew Thompson Guest
|
Posted: Mon Jun 28, 2004 11:52 am Post subject: Re: Pre-draw part of JComponent |
|
|
On 28 Jun 2004 03:08:52 -0700, Lars wrote:
| Quote: | 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.1ewtjet4iz3ls.dlg%4040tude.net>
HTH
--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
|
|
| Back to top |
|
 |
Nigel Wade Guest
|
Posted: Tue Jun 29, 2004 9:32 am Post subject: Re: Pre-draw part of JComponent |
|
|
On Mon, 28 Jun 2004 03:08:52 -0700, Lars wrote:
| Quote: | 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
|
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 );
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : [email]nmw (AT) ion (DOT) le.ac.uk[/email]
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|