I have a list of x,y points which printed,display an uneven peak curve line.
The above image was generated by just painting the points on a java paint component. I used the following way to paint them on a paint component.
g.drawline(pointX,pointY,pointX,pointY)
Are there better ways to paint such 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 some times its flats out and other times they are bizarre.
The simplest way to draw polylines with java.awt.Graphics
is to use the drawPolyline
method. It requires you to have your x and y coordinates stored in separate int[]
arrays, but it is much faster and clearer than to draw each line segment individually.
If you need floating-point coordinates, the best way would be to use a Shape
object with Graphics2D
. Unfortunately, Java does not seem to provide a polyline Shape
implementation, 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 you coordinates, too -- however, it may be more efficient to transform the view, of course.