[英]How can I draw a line between 2 points in Graphics in Java?
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.我有这段代码,lista 是一个 ArrayList 点,每次界面用户拖动鼠标时我都会将其添加到列表中。 But when i do that i get an error: "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 3"
但是当我这样做时,我得到一个错误:“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);
}
}
How can I fix this?我怎样才能解决这个问题?
On your first iteration through this loop, i-1 will be -1 (and i-2 will be -2) this is where you problem is在您通过此循环的第一次迭代中,i-1 将为 -1(而 i-2 将为 -2)这就是您的问题所在
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.