need help to delete the lines and use the shift key to move in JAVA Simple Drawi
ID: 3827921 • Letter: N
Question
need help to delete the lines and use the shift key to move in JAVA
Simple Drawing Application
This project involves writing a JavaFX application which draws short line segments in response to the user typing the arrow keys on the keyboard.
The line starts from the center of the pane and draws in the direction corresponding to which arrow key the user types: RIGHT, UP, LEFT, or DOWN.
If the user types the DELETE key, then all line segments should be erased. The KeyCode constants are defined in the
javafx.scene.input.KeyCode
class.
“Move” vs. “Draw”
If you have time, and would like to try an additional feature, consider this idea:
service the arrow keys will DRAW a line segment in response to the key typed.
feature where, if the user holds down the SHIFT key while hitting an arrow key, then the code does a MOVE to the new X, Y position, without actually drawing anything. Then, if the user types another (unshifted) arrow key, the next line segment will be drawn starting at the new location.
Explanation / Answer
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.stage.Stage; public class extends Application { Pane pane = new Pane(); double width = 400; double height = 400; double cX = width / 2; double cY = height / 2; public void start(Stage primaryStage) { pane.setOnKeyPressed(e -> { switch (e.getCode()) { case UP: moveUp(); break; case DOWN: moveDown(); break; case LEFT: moveLeft(); break; case RIGHT: moveRight(); break; } }); primaryStage.setScene(new Scene(pane, width, height)); primaryStage.setTitle("Click to see position.."); primaryStage.show(); pane.requestFocus(); } private void moveUp() { pane.getChildren().add(new Line(cX, cY, cX, cY - 10)); cY -= 10; } private void moveDown() { pane.getChildren().add(new Line(cX, cY, cX, cY + 10)); cY += 10; } private void moveLeft() { pane.getChildren().add(new Line(cX, cY, cX - 10, cY)); cX -= 10; } private void moveRight() { pane.getChildren().add(new Line(cX, cY, cX + 10, cY)); cX += 10; } public static void main(String[] args) { Application.launch(args); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.