1 Reply Latest reply: Mar 27, 2012 9:02 AM by 921395 RSS

    coordinate system for StackPane

    921395
      Hi,

      I have a task to implement a selection tool - click the mouse to draw connected poligon. But I can not find a connection between the coordinate system and screen pixels. When I try to draw a line as Line.setStartX (xx), Line.setEndX (yy), the line is always drawn from the center. If I try to set the offset of the node as setTranslateX(mm), line is drawn with the center from the current point.. Is any workable example how to add line from start point to the end point to the stackpane?

      Thank you!
        • 1. Re: coordinate system for StackPane
          921395
          I found solution.
          BTW when I try to check doubleclick instead of using Ctrl (i.e. if (me.getClickCount()>1) this is not detect double click when line already painting. It calculates only as single-click.
              public void start(Stage stage) throws Exception {
                  final StackPane root = new StackPane();
                  Scene scene = new Scene(root, 600, 600);
          
                  stage.setScene(scene);
                  stage.setTitle("Selection tool");
          
                  final double centerX = root.getWidth() / 2;
                  final double centerY = root.getHeight() / 2;
          
                  root.setOnMouseMoved(new EventHandler<MouseEvent>() {
          
                      @Override
                      public void handle(MouseEvent me) {
                          if (isDraw && line!=null) {
                              line.setEndX(me.getX()+centerX);
                              line.setEndY(me.getY()+centerY);
                              double midX = (startX+me.getX())/2;
                              double midY = (startY+me.getY())/2;
                              line.setTranslateX(midX-centerX);
                              line.setTranslateY(midY-centerY);
                          }
                      }
                  });
                  root.setOnMouseClicked(new EventHandler<MouseEvent>() {
                      @Override
                      public void handle(MouseEvent me) {
                          if (me.isControlDown())
                          {
                              if (!isDraw) {
                                  isDraw = true;
                              }
                              else
                              {
                                  isDraw = false;
                              }
          
                          }
                          if (isDraw) {
                              startX = me.getX();
                              startY = me.getY();
                              line = new Line();
                              line.setStroke(Color.LIGHTBLUE);
                              line.setStartX(startX+centerX);
                              line.setStartY(startY+centerY);
                              root.getChildren().add(line);
                          }
          
                      }
                  });
          
                  stage.show();
              }
          Edited by: 918392 on 27.03.2012 7:02