Re: Pre-draw part of JComponent
From: Nigel Wade (nmw_at_ion.le.ac.uk)
Date: 06/29/04
- Next message: user_at_domain.invalid: "Re: keyboard handling"
- Previous message: Andrew Thompson: "Re: Sneak Preview - SaverBeans screen savers.."
- In reply to: Lars: "Pre-draw part of JComponent"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 29 Jun 2004 10:32:27 +0100
On Mon, 28 Jun 2004 03:08:52 -0700, Lars wrote:
> 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 : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
- Next message: user_at_domain.invalid: "Re: keyboard handling"
- Previous message: Andrew Thompson: "Re: Sneak Preview - SaverBeans screen savers.."
- In reply to: Lars: "Pre-draw part of JComponent"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|