Add point to graphics area without having to paint the whole area?

Discussion in 'Java' started by Mark, Jun 27, 2005.

  1. Mark

    Mark Guest

    I have written a graphics program that generates a lot of data points
    and as each data point is calculated, it is placed on the chart. I
    decided to add each point as it is calculated, rather than to generate
    all points and show the chart only when all points are done, as a bit of
    eye-candy to keep the user interested.

    The code fragments below shows how it is done:

    ===============
    public void addPoint(DataPoint point)
    {
    // Vector of graph points
    dataPoints.add(point);
    repaint();
    }

    public void paint(Graphics g)
    {
    Graphics2D g2d = (Graphics2D)g;

    // Draw X and Y axes, grid lines, and scale numbers.
    // (some code here)

    // Now draw the graph points in the dataPoints Vector.
    int numPoints = dataPoints.size();
    for (int i = 0; i < numPoints; i++)
    {
    // Various calculations to properply place each point
    // in the g2d space...

    }
    }
    ================

    As each point is generated, it is put into the dataPoints vector
    (through the addPoint() method), and then a repaint() invokes the
    paint() method. Note that the paint() method re-does everything. Even
    if I already have 200 points on the chart, and I add the 201st point,
    all 201 points are redrawn just to achieve the visual effect of adding a
    201st point to the chart.

    As more points are added, things seem slower, since all points have to
    be redrawn in order to add the last new point.

    Question: Is there a technique to just add one point to the EXISTING
    chart without having to redraw the ENTIRE chart?
    Mark, Jun 27, 2005
    #1
    1. Advertising

  2. "Mark" <> wrote in message
    news:izIve.10594$...
    > ...
    > Question: Is there a technique to just add one point to the EXISTING
    > chart without having to redraw the ENTIRE chart?


    Use repaint(int, int, int, int) to only repaint a rectangle that covers
    the new point.

    Inside of paint(Graphics g), only paint points that intersect
    g.getClipBounds().
    Larry Barowski, Jun 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang
    Replies:
    9
    Views:
    421
    S Manohar
    Feb 2, 2004
  2. Replies:
    4
    Views:
    4,628
    Brendan Guild
    Oct 23, 2005
  3. \A_Michigan_User\
    Replies:
    2
    Views:
    887
    \A_Michigan_User\
    Aug 21, 2006
  4. Pkenty
    Replies:
    0
    Views:
    388
    Pkenty
    May 28, 2004
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,322
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page