# How to summarize the logic I used to draw lines as key is pressed?

**URL:** <https://forum.kirupa.com/t/how-to-summarize-the-logic-i-used-to-draw-lines-as-key-is-pressed/676760>\
**Category:** programming\
**Created:** [August 11, 2025, 11:11am UTC](https://forum.kirupa.com/t/how-to-summarize-the-logic-i-used-to-draw-lines-as-key-is-pressed/676760 "2025-08-11T11:11:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![oslon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/oslon/32/24631_2.png) [@oslon](https://forum.kirupa.com/u/oslon)\
**Post date:** [August 11, 2025, 11:11am UTC](https://forum.kirupa.com/t/how-to-summarize-the-logic-i-used-to-draw-lines-as-key-is-pressed/676760/1 "2025-08-11T11:11:57Z")

</div>

I am solving this problem:

> Draw lines using the arrow keys. Write a program that draws line segments using the arrow keys. The line starts from the center of the pane and draws towards east, north, west, or south when the right-arrow key, up-arrow key, left-arrow key, or down-arrow key is pressed.

Here’s my code:

```auto
package com.example.demo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class DisplayMousePosition extends Application {
    Pane pane = new Pane();
    Line line = new Line(300, 100, 200, 100);
    double x1, y1, x2, y2;
    double delta = 50;

    @Override
    public void start(Stage primaryStage) {
        //Line(double startX, double startY, double endX, double endY)
        //Creates a new instance of Line.
        x1 = 200;
        y1 = 100;
        x2 = 300;
        y2 = 100;
        pane.getChildren().add(line);
        pane.setOnKeyPressed(e -> {
            switch (e.getCode()) {
                case DOWN:
                    x1 = x2;
                    y1 = y2;
                    y2 = y1 + delta;
                    pane.getChildren().add(new Line(x1, y1, x2, y2));
                    break;
                case UP:
                    x1 = x2;
                    y1 = y2;
                    y2 = y2 - delta;
                    pane.getChildren().add(new Line(x1, y1, x2, y2));
                    break;
                case LEFT:
                    x1 = x2;
                    y1 = y2;
                    y2 = y1;
                    x2 = x1 - delta;
                    pane.getChildren().add(new Line(x1, y1, x2, y2));
                    break;
                case RIGHT:
                    x1 = x2;
                    y1 = y2;
                    x2 = x1 + delta;
                    y2 = y1;
                    pane.getChildren().add(new Line(x1, y1, x2, y2));
                    break;
            }
        });

        Scene scene = new Scene(pane, 500, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
        line.requestFocus();// for line to receive and accept keyboard input
    }
}

```

I wrote this on my own without using chatgpt or deepseek etc. I know better way of coding this does exist.

However, I want to formally summarize my logic.

 ![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/f/d/fd27b1f08ca2f0a6e736921af03447cfdbc1fa3c.jpeg)

This was how I came to this logic. But I don’t find a simple way to summarize it. Maybe because my logic looks too complicated.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [August 12, 2025, 3:43am UTC](https://forum.kirupa.com/t/how-to-summarize-the-logic-i-used-to-draw-lines-as-key-is-pressed/676760/2 "2025-08-12T03:43:10Z")

</div>

I didn’t look at your logic in great detail, but the main thing to remember is that a handful of points in each direction won’t change. For example, when you are moving the arrow up/down, the X positions will stay the same. The flipside is true when you are moving the arrow left/right where the Y positions will stay the same.

As long as you have that, you are good!
