Menu

[Solved]Java Need Reference Api Lab Sure Javafx Api Opened Standard Java Api Many Javafx Classes S Q37238867

Java

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:

  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.

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.

We will use a Slider to select a size. Create a new Slider, andadd it to the right side of the BorderPane. Use the constructor toset the minimum value to 1, the maximum to 101, and the default to10. Next you want to set the orientation to vertical. Try to figureout how to do this using the JavaFX API.

Second, let’s make the slider look nice by calling the followingmethods, replacing “slider” with whatever you chose to name yourSlider:

slider.setShowTickMarks(true); slider.setShowTickLabels(true); slider.setMajorTickUnit(10); slider.setBlockIncrement(1);

Now change your EventHandler used in setOnMouseDragged to usethe current value of the slider, found by using the getValue methodof Slider for the height and widthof fillOval.

Compile and test your program to see if you can draw!

Expert Answer


Answer to Java You will need to reference the API in this lab. Be sure you have the JavaFX API opened up, and not just the standar… . . .

OR


Leave a Reply

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