Question: Can some edit this code so the program works? Currently whenever I try to draw with the arrow keys it doesn't work because the arrow

Can some edit this code so the program works? Currently whenever I try to draw with the arrow keys it doesn't work because the arrow keys are highlighting the buttons but whenever I remove the buttons to see if the drawing works it does. How would I fix this so the buttons function as they should and allow me to draw with arrow keys?
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ArrowKeyDrawing extends Application {
private double x =100, y =100; // Starting coordinates
private double pixels =10; // Movement increment
private boolean canDraw = true; // Drawing state
@Override
public void start(Stage primaryStage){
Pane root = new Pane();
VBox controls = new VBox(10); // Vertical layout for buttons
Canvas canvas = new Canvas(200,200);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.moveTo(x, y);
gc.setLineWidth(2);
Button btnClear = new Button("Clear");
btnClear.setOnAction(e -> clearCanvas(gc, canvas));
Button btnPencilUp = new Button("Pencil Up");
btnPencilUp.setOnAction(e -> canDraw = false);
Button btnPencilDown = new Button("Pencil Down");
btnPencilDown.setOnAction(e -> canDraw = true);
// Adding buttons to the vertical box
controls.getChildren().addAll(btnClear, btnPencilUp, btnPencilDown);
// Layout
root.getChildren().add(canvas);
root.getChildren().add(controls);
Scene scene = new Scene(root,300,250); // Adjusted for additional UI elements
scene.setOnKeyPressed(e -> handleKeyPress(e.getCode(), gc));
primaryStage.setScene(scene);
primaryStage.setTitle("Drawing With Arrow Keys");
primaryStage.show();
}
private void clearCanvas(GraphicsContext gc, Canvas canvas){
gc.clearRect(0,0, canvas.getWidth(), canvas.getHeight());
gc.beginPath(); // Start a new path after clearing
gc.moveTo(x, y);
}
private void handleKeyPress(KeyCode code, GraphicsContext gc){
if (canDraw){
switch (code){
case UP:
y -= pixels;
break;
case DOWN:
y += pixels;
break;
case LEFT:
x -= pixels;
break;
case RIGHT:
x += pixels;
break;
default:
return; // Ignore other keys
}
gc.lineTo(x, y);
gc.stroke();
} else {
switch (code){
case UP:
y -= pixels;
break;
case DOWN:
y += pixels;
break;
case LEFT:
x -= pixels;
break;
case RIGHT:
x += pixels;
break;
default:
return; // Ignore other keys
}
gc.moveTo(x, y);
}
}
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 Accounting Questions!