2013-07-06 58 views
0

我使用此代碼在拖動鼠標的同時繪製一個矩形。問題是我只能從左到右抽取。用javaFX在各個方向繪製一個矩形失敗

這裏是我的代碼:

public class functionalTest extends Application { 

    BorderPane pane; 
    Rectangle rect; 
    Group group; 
    SimpleDoubleProperty rectinitX = new SimpleDoubleProperty(); 
    SimpleDoubleProperty rectinitY = new SimpleDoubleProperty(); 
    SimpleDoubleProperty rectX = new SimpleDoubleProperty(); 
    SimpleDoubleProperty rectY = new SimpleDoubleProperty(); 


    @Override 
    public void start(Stage stage) { 


     pane = new BorderPane(); 
     Scene scene = new Scene(pane, 800, 600); 
     stage.setScene(scene); 

     group = new Group(); 
     Circle circle = new Circle(200, 200, 25); 
     circle.setFill(Color.HOTPINK); 
     group.getChildren().add(circle); 

     scene.setOnMouseDragged(mouseHandler); 
     scene.setOnMousePressed(mouseHandler); 
     scene.setOnMouseReleased(mouseHandler); 

     rect = getNewRectangle(); 
     rect.widthProperty().bind(rectX.subtract(rectinitX)); 
     rect.heightProperty().bind(rectY.subtract(rectinitY)); 
     pane.getChildren().add(rect); 
     pane.getChildren().add(group); 
     stage.show(); 

     ArrayList<Node> containedNodesArray = new ArrayList<Node>(); 
     containedNodesArray = Main.dragBoxSelection(group, rect); 

     if (containedNodesArray.size() > 0) { 
      System.out.println("Success"); 
     } 
    } 

    EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 

     @Override 
     public void handle(MouseEvent mouseEvent) { 

      if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) { 
       rect.setX(mouseEvent.getX()); 
       rect.setY(mouseEvent.getY()); 
       rectinitX.set(mouseEvent.getX()); 
       rectinitY.set(mouseEvent.getY()); 
      } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) { 
       rectX.set(mouseEvent.getX()); 
       rectY.set(mouseEvent.getY()); 
       System.out.println("RECTX"+rectX.getValue()); 
       System.out.println("RECTY"+rectY.getValue()); 
       ArrayList<Node> containedNodesArray = new ArrayList<Node>(); 
       containedNodesArray = Main.dragBoxSelection(group, rect); 

       if (containedNodesArray.size() > 0) { 
        System.out.println("Success"); 
       } 
      } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) { 
       // Clone the rectangle 
       Rectangle r = getNewRectangle(); 
       r.setX(rect.getX()); 
       r.setY(rect.getY()); 
       r.setWidth(rect.getWidth()); 
       r.setHeight(rect.getHeight()); 
       pane.getChildren().add(r); 

       // Hide the rectangle 
       rectX.set(0); 
       rectY.set(0); 
      } 
     } 


    }; 

    private Rectangle getNewRectangle() { 
     Rectangle r = new Rectangle(); 
     r.setFill(Color.web("blue", 0.1)); 
     r.setStroke(Color.BLUE); 

     return r; 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

請誰能幫助我在其他direcions吸取一直到左,左到右,右到他離開。

+0

我有開發JavaFX中的圖像編輯器中,你只需要有一個反映各個方向繪製您的矩形從哪裏決定,去哪裏我的鼠標。我沒有在這裏的源代碼抱歉,但它並沒有真正編排 –

+0

對不起,但我嘗試了不同的解決方案,以獲得我的鼠標的方向,並設置正確的高度和寬度,但所有那些試驗失敗。你能給我一個提示或者試着給我發送源代碼嗎? – MaryamB

回答

1

這就是你需要做什麼:

當鼠標拖動:拿到dx = mouseEvent.getX() - initial X;如果dx < 0那麼你需要setTranslateX(dx)setWidth(-dx) 其他setTranslateX(0)setWidth(dx)

執行相同的Y座標,但是這一次使用高度不寬;

這就是全部。

0

我取代了代碼是這樣的: 事件處理mouseHandler =新的EventHandler(){

@Override 
    public void handle(MouseEvent mouseEvent) { 

     if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) { 
      rectinitX.set(mouseEvent.getX()); 
      rectinitY.set(mouseEvent.getY()); 
     } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) { 
      Double width = mouseEvent.getX() - rectinitX.getValue(); 
      if (width < 0) { 
       rectX.set(mouseEvent.getX()); 
       rect.setTranslateX(width); 
       rect.widthProperty().bind(rectinitX.subtract(rectX)); 
       System.out.println(rect.getWidth()); 
      } else { 
       rectX.set(mouseEvent.getX()); 
       rect.setTranslateX(0); 
       rect.widthProperty().bind(rectX.subtract(rectinitX)); 
      } 

     } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) { 
      // Clone the rectangle 
      Rectangle r = getNewRectangle(); 
      r.setX(rect.getX()); 
      r.setY(rect.getY()); 
      r.setWidth(rect.getWidth()); 
      r.setHeight(rect.getHeight()); 
      pane.getChildren().add(r); 


     } 
    } 
}; 

我只是想試試您的解決方案在第一寬度vaues但它似乎不工作。你對我的實施有任何意見嗎?

0

您缺少高度參數。

  if (event.getEventType() == MouseEvent.MOUSE_PRESSED) { 
      rect.setX(event.getX()); 
      rect.setY(event.getY()); 
      rectinitX.set(event.getX()); 
      rectinitY.set(event.getY()); 
     } else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) { 
      Double dx = event.getX() - rectinitX.getValue(); 
      Double dy = event.getY() - rectinitY.getValue(); 
      if (dx < 0) { 
       rectX.set(event.getX()); 
       rect.setTranslateX(dx); 
       rect.widthProperty().bind(rectinitX.subtract(rectX)); 
      } else { 
       rectX.set(event.getX()); 
       rect.setTranslateX(0); 
       rect.widthProperty().bind(rectX.subtract(rectinitX)); 
      } 
      if(dy < 0) { 
       rectY.set(event.getY()); 
       rect.setTranslateY(dy); 
       rect.heightProperty().bind(rectinitX.subtract(rectX)); 
      } else { 
       rectY.set(event.getY()); 
       rect.setTranslateY(0); 
       rect.heightProperty().bind(rectY.subtract(rectinitY)); 
      } 

     } else if (event.getEventType() == MouseEvent.MOUSE_RELEASED) { 
      // Clone the rectangle 
      Rectangle r = getNewRectangle(); 
      r.setX(rect.getX()); 
      r.setY(rect.getY()); 
      r.setWidth(rect.getWidth()); 
      r.setHeight(rect.getHeight()); 
      pane.getChildren().add(r); 

      // Hide the rectangle 
      rectX.set(0); 
      rectY.set(0); 
    }}}}; 

這是從左下角向不同的方向拉,除了doesn't工作不幸右頂部確切的代碼,我無法弄清楚。 在默認情況下完美工作,即左至右,但從右至左繪製時存在代碼錯誤。 如果有人能弄明白,I'll不勝appricated

0

應改爲

if(dy < 0) 
{ 
    rectY.set(mouseEvent.getY()); 
    rect.setTranslateY(dy); 
    rect.heightProperty().bind(rectinitY.subtract(rectY)); 
}