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 y ; Starting coordinates
private double pixels ; Movement increment
private boolean canDraw true; Drawing state
@Override
public void startStage primaryStage
Pane root new Pane;
VBox controls new VBox; Vertical layout for buttons
Canvas canvas new Canvas;
GraphicsContext gc canvas.getGraphicsContextD;
gcmoveTox y;
gcsetLineWidth;
Button btnClear new ButtonClear;
btnClear.setOnActione clearCanvasgc canvas;
Button btnPencilUp new ButtonPencil Up;
btnPencilUp.setOnActione canDraw false;
Button btnPencilDown new ButtonPencil Down";
btnPencilDown.setOnActione canDraw true;
Adding buttons to the vertical box
controls.getChildrenaddAllbtnClear btnPencilUp, btnPencilDown;
Layout
root.getChildrenaddcanvas;
root.getChildrenaddcontrols;
Scene scene new Sceneroot; Adjusted for additional UI elements
scene.setOnKeyPressede handleKeyPressegetCode gc;
primaryStage.setScenescene;
primaryStage.setTitleDrawing With Arrow Keys";
primaryStage.show;
private void clearCanvasGraphicsContext gc Canvas canvas
gcclearRect canvas.getWidth canvas.getHeight;
gcbeginPath; Start a new path after clearing
gcmoveTox y;
private void handleKeyPressKeyCode 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
gclineTox y;
gcstroke;
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
gcmoveTox y;
public static void mainString args
launchargs;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
