我必须在jframe中绘制鼠标线。这是我的方法PaintComponent:
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) {
for (int index = 0; index < (pointCollection.size() - 1); index++){
line2d=new Line2D.Double(pointCollection.get(index),
pointCollection.get(index + 1));
//repaint();
}
}