Re: Pre-draw part of JComponent

From: Nigel Wade (nmw_at_ion.le.ac.uk)
Date: 06/29/04


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


Relevant Pages

  • Re: How do I create graph paper in Word or Excel?
    ... graph in a couple of minutes. ... Bring up the Draw menu by selecting View/Toolbars/Drawing. ... Ctrl key and drag a new line to the right one grid space. ... Deselect the Select Objects tool and add any titles you desire, ...
    (microsoft.public.word.drawing.graphics)
  • Re: Some advice creating a graph class
    ... >I am developing a program to draw a graph of database data using the ... Are you trying to draw a grid like this? ... That screen shot is of a CView drawn to reperesent a database. ...
    (microsoft.public.vc.mfc)
  • RE: How do I create graph paper in Word or Excel?
    ... How to create graph paper in Word ... Bring up the Draw menu by selecting View/Toolbars/Drawing. ... Ctrl key and drag a new line to the right one grid space. ... Deselect the Select Objects tool and add any titles you desire, ...
    (microsoft.public.word.drawing.graphics)
  • Re: Z-80 coprocessor cards
    ... DR GRAPH provides business graphics, ... it can take data from many popular spreadsheet ... DR DRAW is a very flexible free-form drawing and drafting ...
    (comp.os.cpm)
  • Re: Which component to use to display mathematical graphs with VB6
    ... Your code can then draw into that Form's client area (the area ... inside its borders) or alternatively into a PictureBox that you can ... generally very fast at drawing a set of many lines in one call using a ... Do you mean you want your graph to cover all of the available screen ...
    (comp.lang.basic.visual.misc)