java java从点的arraylist中绘制折线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7556837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java draw polyline from an arraylist of points
提问by trs
Is it possible to draw a polyline by passing the method an array list of Point values? something like this:
是否可以通过向方法传递点值的数组列表来绘制折线?像这样:
ArrayList<Point> projectilePoints=new ArrayList<Point>();
Projectile p = new Projectile(11, 17, 73, 37);
for (int i = 0; i < 11; i++) {
Point point = p.getPositionAt(i);
projectilePoints.add(point);
}
g.drawPolyline(projectilePoints, projectilePoints, 11);
What is the correct way to pass in the parameters of x and y points for the polyline?
为折线传入 x 和 y 点的参数的正确方法是什么?
回答by adatapost
No, there is no such method takes Arraylist
of Point
reference parameter. The Syntax is,
否,不存在这样的方法需要Arraylist
的Point
参考参数。语法是,
Graphics.drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
Graphics.drawPolyline(int[] xPoints, int[] yPoints, int nPoints)
回答by Simon C
Call method Graphics2D.drawPolyline
. This method takes an int array of X coordinate values, an int array of Y coordinate values and the number of points.
调用方法Graphics2D.drawPolyline
。此方法采用 X 坐标值的 int 数组、Y 坐标值的 int 数组和点数。
There is no line drawing method that takes Point
objects, you have to create int arrays of coordinates.
没有采用Point
对象的线条绘制方法,您必须创建坐标的 int 数组。
See http://download.oracle.com/javase/1,5.0/docs/api/java/awt/Graphics2D.html
见http://download.oracle.com/javase/1,5.0/docs/api/java/awt/Graphics2D.html
回答by Thomas
The JavaDpc on Graphics#drawPolyLine
states that you need to pass 2 int arrays that represent the x and y coordinates.
JavaDpc 上Graphics#drawPolyLine
声明您需要传递 2 个表示 x 和 y 坐标的 int 数组。
Alternatively, you might use Graphics2d#draw(Shape)
and pass a Path2D
shape, that can be prefilled using your points (e.g. by calling lineTo(x,y)
for all points but the first - for which you might call moveTo(x,y)
).
或者,您可以使用Graphics2d#draw(Shape)
并传递一个Path2D
形状,该形状可以使用您的点进行预填充(例如,通过调用lineTo(x,y)
除第一个点之外的所有点 - 您可能会调用moveTo(x,y)
)。