Question

Java:

Exercise 15.9 Follow the instructions in the textbook, also shown here: 15.9 (Draw lines using the arrow keys) Write a progra

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Exercise15_09.java

import javafx.application.Application;

import static javafx.application.Application.launch;

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.scene.paint.Color;

import javafx.stage.Stage;

public class Exercise15_09 extends Application {

    //starting position

    private double x = 100, y = 100;

    //represents the length of line created for a single arrow key press

    //you can modify this as you like

    private double displacement = 10;

    @Override

    public void start(Stage primaryStage) {

        //creating a Pane

        Pane root = new Pane();

        //creating a Canvas

        Canvas canvas = new Canvas(600, 600);

        //getting a GraphicsContext from canvas

        GraphicsContext gc = canvas.getGraphicsContext2D();

        //positioning gc to (x,y)

        gc.moveTo(x, y);

        //using black fill color

        gc.setFill(Color.BLACK);

        //using 5 for line thickness

        gc.setLineWidth(5);

        //adding canvas to root

        root.getChildren().add(canvas);

        //creating a Scene

        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) {

                //moving up

                gc.lineTo(x, y - displacement);

                //updating coordinates

                y = y - displacement;

            } else if (c == KeyCode.DOWN) {

                //moving down

                gc.lineTo(x, y + displacement);

                y = y + displacement;

            } else if (c == KeyCode.LEFT) {

                //moving left

                gc.lineTo(x - displacement, y);

                x = x - displacement;

            } else if (c == KeyCode.RIGHT) {

                //moving right

                gc.lineTo(x + displacement, y);

                x = x + displacement;

            }

            //stroking, to show the drawing

            gc.stroke();

        });

        //setting up scene and displaying

        primaryStage.setScene(scene);

        primaryStage.setTitle("Exercise15_09");

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

/*OUTPUT*/


Exercise15_09 X

Add a comment
Know the answer?
Add Answer to:
Java: Exercise 15.9 Follow the instructions in the textbook, also shown here: 15.9 (Draw lines using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge...

    Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge sort) Revise Listing 30.10, ParallelMergeSort.java, to define a generic parallelMergeSort method as follows: public static <E extends Comparable<E>> void parallel MergeSort(E list) In the main) method, create an array of integers, print the array of integers, then use the parallelMergeSort() on the array, and print the sorted array. Then, repeat the steps for an array of strings Exercise 32.13 Follow the instructions in...

  • Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel...

    Java Programming Exercise 32.13 Follow the instructions in the textbook, also shown here: 32.13 (Generic parallel merge sort) Revise Listing 30.10, ParallelMergeSort.java, to define a generic parallelMergeSort method as follows: public static <E extends Comparable<E>> void parallel MergeSort(E list) In the main) method, create an array of integers, print the array of integers, then use the parallelMergeSort() on the array, and print the sorted array. Then, repeat the steps for an array of strings

  • Constructing a Topographic Map 7.7 Construct a simple topographic map by drawing contour lines. Early topographic...

    Constructing a Topographic Map 7.7 Construct a simple topographic map by drawing contour lines. Early topographic maps were constructed by first surveying an area and establishing the elevations at numerous locations. The surveyor then sketched contour lines on the map by estimating their position between the points of known elevation Today, topographic maps are made by computers from radar data or a series of stereoscopic aerial photographs similar to those in Figure 7.2. The data from these images is processed...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT