[Solved]Java Please Need Reference Api Lab Sure Javafx Api Opened Standard Java Api Many Javafx Cl Q37273584
In Java please!
You will need to reference the API in this lab. Be sure you havethe JavaFX API opened up, and not just the standard JavaAPI. Many of the JavaFX classes have similar names to classes inthe standard API.
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:
- Have the Painter class implementEventHandler<MouseEvent>.
- Create a nested class that implementsEventHandler<MouseEvent>.
- Create an anonymous class that extendsEventHandler<MouseEvent>.
The body of the handle method (or the body of the “lambda”shortcut, or the body of your method reference) should do thefollowing:
- Get the GraphicsContext for the Canvas. Canvas has a methodgetGraphicsContext2D that returns the GraphicsContext.
- Set the color to draw by using the setFill method of theGraphicsContext. For now, set the color to Color.BLACK.
- 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.
Now let us add some color to the pen. JavaFX has a gadget calledthe ColorPicker that lets a user choose a color.
Create a new ColorPicker and use the constructor that sets theinitial color to Color.BLACK. Then place the ColorPicker at the topof your BorderPane.
Now change the EventHandler you use in the setOnMouseDraggedmethod to have the GraphicsContext use the color from theColorPicker rather than always using Color.BLACK. Use the methodgetValue to get the current selected color from theColorPicker.
Expert Answer
Answer to In Java please! You will need to reference the API in this lab. Be sure you have the JavaFX API opened up, and not just … . . .
OR

