2015-09-14 12 views
0

我绘制了大量可以移动的行,可以调整大小。但是,一旦我点击之前创建的行,它就不会移动。只有最后创建的行正在移动。所有建议我的线随时移动。摆动 - 从多行中识别一行

MouseAdapter mouseAdapter = new MouseAdapter() { 

     public void mousePressed(MouseEvent e) { 
      Rectangle rec = new Rectangle(e.getX(), e.getY(), 50, 50); 
      prevPoint = e.getPoint(); 
      updateAction(e); 
      ignoreMoves = true; 

      System.out.println("Size:"+shapes.size()); 

      for (Shape shape : shapes) { 
       if(shape.contains(prevPoint)){ 
        System.out.println("Clicked Position is within a shape"); 
        currentShape=shape; 
       } 
       else{ 
        System.out.println("Clicked Position NOT within a shape"); 
       } 
      } 
      repaint(); 
     } 

     public void mouseDragged(MouseEvent e) { 
      System.out.println("mouseDragged action:" + action); 
      if (action != null) { 
       switch (action) { 
       case Move: { 
        //System.out.println("Move"); 
        int dx = 0; 
        int dy = 0; 

        dx = (int) (prevPoint.x - e.getPoint().getX()); 
        dy = (int) (prevPoint.y - e.getPoint().getY()); 

        Line2D shape = (Line2D) currentShape; 

        int x1 = (int) (shape.getX1() - dx); 
        int y1 = (int) (shape.getY1() - dy); 

        int x2 = (int) (shape.getX2() - dx); 
        int y2 = (int) (shape.getY2() - dy); 

        Point startPoint = new Point(x1, y1); 
        Point endPoint = new Point(x2, y2); 

        if (shape != null) { 
         shape.setLine(startPoint, endPoint); 
         prevPoint = e.getPoint(); 
         repaint(); 
        } 
       } 
        break; 
       case ResizeEast: { 
         Line2D shape = (Line2D) currentShape; 
        Point endPointLine = e.getPoint(); 
        shape.setLine(shape.getP1(), endPointLine); 
        repaint(); 
       } 
        break; 
       } 
      } 
     } 

     public void mouseMoved(MouseEvent e) { 
      if (!ignoreMoves) { 
       updateAction(e); 
      } 
     }; 

     protected void updateAction(MouseEvent e) { 
      int x = e.getX(); 
      int y = e.getY(); 

      Point clickedPoint = new Point(x, y); 

      int width = 0; 
      int height = 0; 

      double startPointX = 0; 
      double startPointY = 0; 
      double endPointX = 0; 
      double endPointY = 0; 

      if (shapes != null && shapes.size() > 0) { 
        Line2D shape = (Line2D) currentShape; 

       Line2D line = (Line2D) shape; 
       startPointX = line.getX1(); 
       startPointY = line.getY1(); 
       endPointX = line.getX2(); 
       endPointY = line.getY2(); 

       Rectangle rect = shape.getBounds(); 
       width = rect.width; 
       height = rect.height; 


       if (x > endPointX - 5) { 
        action = MouseAction.ResizeEast; 
       } else if (rect.contains(clickedPoint)) { 
        action = MouseAction.Move; 
       } else { 
        action = null; 
       } 
      } 

      if (action != null) { 
       setCursor(action.getCursor()); 
      } else { 
       setCursor(null); 
      } 
     } 

    }; 
    protected void paintComponent(Graphics g) { 
     //System.out.println("Paint"); 

     Graphics2D g2d = (Graphics2D) g; 
     g2d.setPaint(Color.BLACK); 

     if (shapes != null && shapes.size() > 0) { 

      for (Shape shape : shapes) { 
       g2d.draw(shape); 
      } 
     } else { 
      System.out.println("Shapes is Null"); 
     } 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Point startPoint = new Point(50, 200); 
     Point endPoint = new Point(100, 200); 
     currentShape = new Line2D.Double(startPoint, endPoint); 
     shapes.add(currentShape); 
     repaint(); 
    } 

回答

1

你的问题是Line2D。 contains

如果你阅读文档,它说:

返回:假的,因为Line2D不包含任何区域。

我推荐的解决方案是创建rectangle2D而不是Line2Ds,其中宽度或高度基本上是Stroke大小(请参阅Stroke)。此外,你必须旋转你的Rectangle2Ds。最简单的方法可能是将它们转换为Area s(请参见区域),这是我创建时使用的格式。similar type of shape container

+0

我还希望随鼠标的运动改变线的宽度旋转..也可以通过你的方法来实现...?还有,对于所有这些旋转和宽度变化(使用鼠标)和设置每行的属性,最好的方法是什么。 –

+0

@RajTrivedi为什么你不坚持你的原始目标,直到你得到那个工作? – ControlAltDel

+0

Rectangle2D我不能使用,因为我需要绘制2点之间的边缘... Rectangle2D需要一个点和宽度,这不会帮助我的原因.. –