How to draw curve line (peaks) using x, y coordinate list

I have a list of x, y points that are printed, displaying a jagged curved line of a curve.

enter image description here

The above image was created by painting dots in java paint component. I used the following method to paint them onto the paint component.

g.drawline(pointX,pointY,pointX,pointY)

      

Are there any better ways to draw such a wave line? I checked some of the similar questions, often they need to print a curve or peak, but my line is not always a peak as several times his flat, and in other cases they are strange.

+3


source to share


1 answer


The easiest way to draw polylines with java.awt.Graphics

is to use the drawPolyline

. This requires your x and y coordinates to be stored in separate arrays int[]

, but they are much faster and easier to understand than drawing each segment separately.

If you need floating point coordinates, the best way would be to use an object Shape

with Graphics2D

. Unfortunately Java doesn't seem to provide a polyline implementation Shape

, but you can easily use Path2D

:



Graphics2D graphics = /* your graphics object */;
double[] x = /* x coordinates of polyline */;
double[] y = /* y coordinates of polyline */;

Path2D polyline = new Path2D.Double();
polyline.moveTo(x[0], y[0]);
for (int i = 1; i < x.length; i++) {
    polyline.lineTo(x[i], y[i]);
}

graphics.draw(polyline);

      

This way allows you to easily transform your coordinates, however it's probably more efficient to transform the view, of course.

+2


source







All Articles