如何在Java中的图形中画两点之间的线?
我有这段代码,lista 是一个点数组列表,每次界面用户拖动鼠标时我都会将其添加到列表中。但是当我这样做时,我收到错误:“AWT-EventQueue-0”java.lang.IndexOutOfBoundsException:索引-1超出长度3的范围”
for(int i=0;i<lista.size();i++){
g.fillOval(lista.get(i).x,lista.get(i).y,radio*2,radio*2);
if(lista.size()>1){
g.drawLine(lista.get(i-1).x,lista.get(i-1).y,lista.get(i-2).x,lista.get(i-2).y);
}
}
我该如何解决这个问题?
I have this code, lista is an ArrayList of Points that i add to the list everytime the user of the interface drags the mouse. But when i do that i get an error: "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3"
for(int i=0;i<lista.size();i++){
g.fillOval(lista.get(i).x,lista.get(i).y,radio*2,radio*2);
if(lista.size()>1){
g.drawLine(lista.get(i-1).x,lista.get(i-1).y,lista.get(i-2).x,lista.get(i-2).y);
}
}
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这个循环的第一次迭代中, i-1 将是 -1 (而 i-2 将是 -2 ),这就是你的问题所在
On your first iteration through this loop, i-1 will be -1 (and i-2 will be -2) this is where you problem is