Question: Edit this code so it has 3 buttons, one to clear the pane, a button to stop drawing but allow movement, and a button to

Edit this code so it has 3 buttons, one to clear the pane, a button to stop drawing but allow movement, and a button to draw while moving (like a pencil going up button and down button). If possible make multiple methods so it isn't one start method, will thumbs up thank you.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class ArrowKeyDrawing extends Application {
private double x =100, y =100;
private double pixels =10;
@Override
public void start(Stage primaryStage){
Pane root = new Pane();
Canvas canvas = new Canvas(200,200);
//getting a GraphicsContext from canvas
GraphicsContext gc = canvas.getGraphicsContext2D();
//positioning gc to (x,y)
gc.moveTo(x, y);
//line thickness
gc.setLineWidth(2);
//adding canvas to root
root.getChildren().add(canvas);
Scene scene = new Scene(root);
//adding key pressed listener
scene.setOnKeyPressed(e ->{
//getting clicked key code
KeyCode c = e.getCode();
//finding clicked key
if (c == KeyCode.UP){
//up
gc.lineTo(x, y - pixels);
//updating coordinates
y = y - pixels;
} else if (c == KeyCode.DOWN){
//down
gc.lineTo(x, y + pixels);
y = y + pixels;
} else if (c == KeyCode.LEFT){
//left
gc.lineTo(x - pixels, y);
x = x - pixels;
} else if (c == KeyCode.RIGHT){
//right
gc.lineTo(x + pixels, y);
x = x + pixels;
}
//show the drawing
gc.stroke();
});
primaryStage.setScene(scene);
primaryStage.setTitle("Drawing With Arrow Keys");
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!