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

M

Mark

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?
 
L

Larry Barowski

Mark said:
...
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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top