Menu

[Solved]Java Create New Class Called Painter Extends Application Add Borderpane Scene Add Scene St Q37276983

Java.

Create a new class called Painter that extends Application. Adda BorderPane to the Scene, add the Scene to the Stage, show theStage and create a main method that calls Application.launch(…).If you do not recall how to do this, look at your prelab or Lab8.

Once you have the JavaFX application set up, create aCanvas object and add it to the center of the BorderPane. I suggestmaking the Canvas a large size like 500×500 or bigger. (Our canvasis not going to be resizable in this lab, so you want something bigenough to work with.)

Now we want to be able to draw on the canvas by clicking anddragging on it with a mouse. For this you need to add anEventHandler to your canvas that listens for MouseEvents. You wantto call canvas.setOnMouseDragged(…) where canvas is the variablestoring your Canvas object, and the input to the method is aEventHandler<MouseEvent> object. There are multiple ways youcan do this:

  1. Have the Painter class implementEventHandler<MouseEvent>.
  2. Create a nested class that implementsEventHandler<MouseEvent>.
  3. Create an anonymous class that extendsEventHandler<MouseEvent>.
  4. Use the “lambda” shortcut that provides the body of what youwant to occur in the handle method of EventHandler.
  5. Write a method that takes a MouseEvent as input and use the”method reference” shortcut

The body of the handle method (or the body of the “lambda”shortcut, or the body of your method reference) should do thefollowing:

  1. Get the GraphicsContext for the Canvas. Canvas has a methodgetGraphicsContext2D that returns the GraphicsContext.
  2. Set the color to draw by using the setFill method of theGraphicsContext. For now, set the color to Color.BLACK.
  3. Draw a circle on the Canvas at the current location of themouse. Use the fillOval method of the GraphicsContext context. Setthe width and height parameters to 10 and get the x and ycoordinates from the MouseEvent that is the parameter of themethod.

Expert Answer


Answer to Java. Create a new class called Painter that extends Application. Add a BorderPane to the Scene, add the Scene to the St… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *