如何使用x,y坐标列表绘制曲线线(峰) [英] How to Drawing a curve line(peaks) using list of x,y coordinates
问题描述
我有一个打印的x,y点列表,显示不均匀的峰曲线.
I have a list of x,y points which printed,display an uneven peak curve line.
上面的图像是通过仅在Java paint组件上绘制点而生成的.我使用以下方法将它们绘制在绘制组件上.
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.
推荐答案
使用java.awt.Graphics
绘制折线的最简单方法是使用drawPolyline
方法.它要求您将x和y坐标存储在单独的int[]
数组中,但是比单独绘制每个线段要快得多,而且更清晰.
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.
如果需要浮点坐标,最好的方法是使用 Shape
对象与Graphics2D
.不幸的是,Java似乎没有提供折线Shape
实现,但是您可以轻松地使用
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.
这篇关于如何使用x,y坐标列表绘制曲线线(峰)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!