This project involves writing a JavaFX application which draws short line segmen
ID: 3835752 • Letter: T
Question
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 "Move" vs "Draw" If you have time, and would like to try an additional feature, consider this idea: Normally, your code to service the arrow keys will DRAW a line segment in response to the key typed. You could also add a a 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. If you have another enhancement in mind, please discuss it with me after class, so we can agree on a reasonable scope for the project. A sample of the display is shown below, with optional Enhancement #1 implemented:Explanation / Answer
------------------------------------------------------------------------------------------ Main.java ------------------------------------------------------------------------------------------ package sample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.shape.Line; import javafx.stage.Stage; /** * this class draws lines on arrow key prompts * also combined shift key + arrow keys just moves the curson, doesnt draw * delete key clears the pane */ public class Main extends Application { Pane p = new Pane(); double wid = 400; double ht = 400; double x1 =200,y1=200; @Override public void start(Stage primaryStage) throws Exception { p.setOnKeyPressed(e -> { switch (e.getCode()) { case UP: if(e.isShiftDown()){ //checking composite (shift key condition) y1 = y1 - 10; //just update the position. not draw }else { p.getChildren().add(new Line(x1, y1, x1, y1 - 10)); //draw y1 = y1 - 10; //update the position } break; case DOWN: if(e.isShiftDown()){//checking composite (shift key condition) y1 = y1 + 10;//just update the position. not draw }else { p.getChildren().add(new Line(x1, y1, x1, y1 + 10));//draw y1 = y1 + 10; //update the position } break; case LEFT: if(e.isShiftDown()){//checking composite (shift key condition) x1 = x1 - 10;//just update the position. not draw }else { p.getChildren().add(new Line(x1, y1, x1 - 10, y1));//draw x1 = x1 - 10;//update the position } break; case RIGHT: if(e.isShiftDown()){//checking composite (shift key condition) x1 = x1 + 10;//just update the position. not draw }else { p.getChildren().add(new Line(x1, y1, x1 + 10, y1));//draw x1 = x1 + 10;//update the position break; } case DELETE: p.getChildren().clear(); x1 =200;y1=200; //reset to initial position break; } }); primaryStage.setScene(new Scene(p,wid,ht)); primaryStage.show(); primaryStage.setTitle("Project 3 Solution"); p.requestFocus(); } /** * launch the application * @param args */ public static void main(String[] args) { Application.launch(args); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.