Swing 挥杆中的自由手线-消除错误”;索引越界异常“;

Swing 挥杆中的自由手线-消除错误”;索引越界异常“;,swing,lines,indexoutofboundsexception,curves,Swing,Lines,Indexoutofboundsexception,Curves,我必须在JFRame中用鼠标画线。 以下是我的方法组件: public void paintComponent(Graphics g){ Graphics2D g2d = (Graphics2D) g; if(pointCollection.get(0)!=null && pointCollection.get(pointCollection.size())!=null){ g2d.setPaint(Color.BLUE); g2d.setStroke(new Bas

我必须在JFRame中用鼠标画线。 以下是我的方法组件:

public void paintComponent(Graphics g){
 Graphics2D g2d = (Graphics2D) g;
 if(pointCollection.get(0)!=null && pointCollection.get(pointCollection.size())!=null){
  g2d.setPaint(Color.BLUE);
  g2d.setStroke(new BasicStroke(1.5f));
  g2d.draw(line2d);
 }
}
它基于我从MouseMotionListener和MouseListener接口获得的实现方法

public void mouseDragged(MouseEvent arg0) {

pointCollection = new ArrayList<Point>(50);
pointCollection.add(arg0.getPoint());
  for (int index = 0; index < pointCollection.size(); index++){
    line2d=new Line2D.Double(pointCollection.get(index), pointCollection.get(index+1));
   //repaint();
  }
 }
public void mouseDragged(MouseEvent arg0){
pointCollection=新阵列列表(50);
添加(arg0.getPoint());
对于(int index=0;index
这个想法是收集点,然后在它们之间画线,这样我就得到了一条曲线而不是直线。你能帮我找出我犯的逻辑错误吗


谢谢大家!

您已经过了收藏的末尾

public void mouseDragged(MouseEvent arg0) {

  for (int index = 0; index < (pointCollection.size() - 1); index++){
    line2d=new Line2D.Double(pointCollection.get(index), 
        pointCollection.get(index + 1));
   //repaint();
  }
public void mouseDragged(MouseEvent arg0){
对于(int index=0;index<(pointCollection.size()-1);index++){
line2d=新的line2d.Double(pointCollection.get(index),
get(索引+1));
//重新油漆();
}
}