you can construct a rectangle just contains the line. So rect has same length with line, and height/weight is 3 pixels.     ........   ...
     .------.   .|.
     ........   .|.
                ...Polygon rect=new Polygon(int[] xpoints,int[] ypoints); 
rect.contains(x,y) to determine if the line has been selected.int[] xpoints and int[] ypoints should be created carefully, as the line can has different directions.

解决方案 »

  1.   

    Got your idea, but I am too stupid to make it work.Go on trying this.Thanks anyway.
      

  2.   

    public java.awt.Rectangle getBounds(Line2D.Double line) {       //他妈的是直线的时候bounds为0
    //       Line2D.Double templine = line;
    //       if (line.x1 == line.x2) {
    //         templine = new Line2D.Double(line.x1 + 3, line.y1, line.x2 - 3,
    //                                      line.y2);
    //       }
    //       if (line.y1 == line.y2) {
    //         templine = new Line2D.Double(line.x1, line.y1 + 3, line.x2,
    //                                      line.y2 - 3);
    //       }
           return getBounds(templine.getBounds());
         }注释部分为在直线的时候,返回偏移量!
    这个点中的范围大,且简单!但是还是不购准确!
      

  3.   

    /**
     * 判断一个点是否在某个链接线上
     * @see inforway.topo.NetNode#isPoint(java.awt.Point)
     */
    public boolean isPoint(Point point) {
    //得到两点之间的距离的一半
    double a=(Math.sqrt((pointf.x-pointl.x)*(pointf.x-pointl.x)+
    (pointf.y-pointl.y)*(pointf.y-pointl.y)))/2;

    double c=Math.sqrt((pointf.x-point.x)*(pointf.x-point.x)+
    (pointf.y-point.y)*(pointf.y-point.y));

    double b=Math.sqrt(c*c-a*a);

    if(b<=2){   //这个值2可以选择点与直线的距离
    return true;
    }
    else{
    return false;
    }
    }
      

  4.   

    谢谢各位高手的帮助,都很棒很聪明,只是pqds地返回的不对,我是用getBounds.contain(x,y)来判断点击的点是否在矩形内,但是不返回true.不过现在已经解决了,点击的很准,代码如下,是我根据楼上的改写的,大家实在很聪明。
    过一会结贴给分,谢谢大家!public boolean lineHit(int x,int y)
    {
      for(int i=0;i<numOfNodes;i++)
        for(int j=0;j<numOfNodes;j++)
        {
          double x1,x2,y1,y2,k;
          x1=node[i].x;
          x2=node[j].x;
          y1=node[i].y;
          y2=node[j].y;                    double a=Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
          double c=Math.abs((y2-y1)*x-(x2-x1)*y+(x2-x1)*y1-(y2-y1)*x1);
          double b=c/a;      if((weight[i][j]>0)&&(b<=3))
          {
             return true;
           }     }
      return false;
    }