How to Drawing a curve line(peaks) using list of x,y coordinates

て烟熏妆下的殇ゞ 提交于 2019-12-23 17:10:00

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31508011/how-to-drawing-a-curve-linepeaks-using-list-of-x-y-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!