title: JavaFX Line segment width
body: <p>I have some code which responds to key presses, and draws points and line segments correspondingly. However, the width of the line segments seems to alternate, even though I don't touch the line stroke width in the code. I'm wondering why this is happening.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.layout.Pane;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.Rectangle;
  import javafx.stage.Stage;
  import javafx.scene.shape.Line;
  
  public class DrawLineSegments extends Application {
  
      static double x = 0.0;
      static double y = 0.0;
  
      public static void main(String[] args) {
          Application.launch(args);
      }
  
      @Override
      public void start(Stage primaryStage) throws Exception {
  
          Pane p = new Pane();
  
          Rectangle border = new Rectangle(0, 0, 300, 100);
          border.setFill(Color.TRANSPARENT);
          Rectangle initialPoint = new Rectangle(border.getWidth() / 2, border.getHeight() / 2, 2, 2);
          initialPoint.setFill(Color.BLACK);
  
          x = border.getWidth() / 2;
          y = border.getHeight() / 2;
  
          p.getChildren().addAll(border, initialPoint);
  
          p.setOnKeyPressed(e -&gt; {
  
              switch (e.getCode()) {
  
              case UP : 
                  Line upLine = new Line(x + 1, y + 1, x + 1, y + 1 - 7.5);
                  y = y - 7.5;
                  upLine.setStroke(Color.BLUEVIOLET);
                  upLine.setFill(Color.SANDYBROWN);
                  p.getChildren().add(upLine);
                  p.getChildren().add(new Rectangle(x, y, 2, 2));
                  break;
  
              case DOWN : 
                  Line downLine = new Line(x + 1, y + 1, x + 1, y + 1 + 7.5);
                  y = y + 7.5;
                  downLine.setStroke(Color.BLUEVIOLET);
                  downLine.setFill(Color.SANDYBROWN);
                  p.getChildren().add(downLine);
                  p.getChildren().add(new Rectangle(x, y, 2, 2));
                  break;
  
              case LEFT : 
                  Line leftLine = new Line(x - 7.5, y + 1, x + 1, y + 1);
                  x = x - 7.5;
                  leftLine.setStroke(Color.BLUEVIOLET);
                  leftLine.setFill(Color.SANDYBROWN);
                  p.getChildren().add(leftLine);
                  p.getChildren().add(new Rectangle(x, y, 2, 2));
                  break;
  
              case RIGHT : 
                  Line rightLine = new Line(x + 7.5, y + 1, x + 1, y + 1);
                  x = x + 7.5;
                  rightLine.setStroke(Color.BLUEVIOLET);
                  rightLine.setFill(Color.SANDYBROWN);
                  p.getChildren().add(rightLine);
                  p.getChildren().add(new Rectangle(x, y, 2, 2));
                  break;
  
              default:
                  break;
              }
          });
  
          Scene scene = new Scene(p);
  
          primaryStage.setScene(scene);
          primaryStage.setTitle("Draw Line Segments");
          primaryStage.show();
  
          p.requestFocus();
  
      }
  
  }
  </code></pre>
  
  <p>And an image that perhaps better explains what I'm asking:</p>
  
  <p><img src="https://i.stack.imgur.com/asipM.png" alt="enter image description here"></p>