Question: Program Description Write a Java program that constructs a Graphical User Interface using JavaFX. The GUI should two Combo Boxes, one to select a color,
Program Description Write a Java program that constructs a Graphical User Interface using JavaFX. The GUI should two Combo Boxes, one to select a color, and one to select a thickness of lines. It should also have two buttons, one is to undo (erase) the last drawn line, and one is to erase all lines. (The size of the applet here is approximately 400 X 400). A user can choose a color using a combo box. Until the color is changed, any line will be drawn with that color. The default color is black. The color combo box should have choices of Black, Blue, Red, Yellow, and Green. A user can choose a width of line strokes using a combo box. Until the width is changed, any line will be drawn with that width. The default color is 1. The width combo box should have choices of 1, 3, 5, and 7. A user can move a mouse into the drawing area and press the mouse button, and this point will be the starting point of a line. A user can drag a mouse, and one should see a line from the pressed point to the point where a user is dragging. Once a user releases the mouse button. that line between the pressed point and released point should be drawn on the drawing pane permanently. A user can again presses, drags and releases the mouse to draw another line with a choice of color and size at that time. If a user presses the Undo button, then the last drawn line should disappear. If a user pressed the Erase button, all drawn lines should disappear. Class description DrawingPane class The DrawingPane class organizes all components in the GUI. It should contain a Pane (drawing area) at the bottom, and another pane containing two combo boxes, one for colors, one for stroke width, and undo button and erase button. Your pane needs to have the same appearance as the shown picture. Attribute name Attribute type Description colorCombo ComboBox A combo box to choose a color from black, blue, red, yellow, and green. widthCombo ComboBox A combo box to choose a width of line strokes, fro 1, 3, 5, and 7. undoButton Button A button to erase the last drawn line. eraseButton Button A button to erase all drawn lines. canvas Pane A pane where a user can draw lines. lineList ArrayList This is an ArrayList which we will use to store all drawn lines. This class should have a constructor: public DrawingPane() This is where all components are arranged. Add as many instance variables as you like to this class, and initialize them in this constructor. Then they need to be arranged so that the GUI will have the required appearance in this constructor. Objects of the Handlers are also instantiated here and are used to set for their corresponding component(s). MouseHandler class (defined as a nested class of DrawingPane class) The MouseHandler class implements EventHandler interface. It must implement the following method: public void handle(MouseEvent event) This method should work with the canvas pane. If the mouse button is pressed, and is dragged, then it should show a temporary line using the pressed point and the dragging point (thus while the mouse is dragged, this line will keep moving since the dragged point is moving.) When the mouse button is released, a permanent line using the pressed point and the released point will be drawn. If a user pressed and released the mouse multiple times, then multiple lines will be drawn. Each line should be drawn using the selected color and the selected stroke with at that time. ButtonHandler class (defined as a nested class of DrawingPane class) The ButtonHandler class implements EventHandler interface. It must implement the following method: public void handle(ActionEvent event) This method should work with the undoButton and eraseButton. If the undoButton is pushed, then it should delete the last drawn line, and if the eraseButton is pushed, then it should erase all drawn lines. ColorHandler class (defined as a nested class of DrawingPane class) The ColorHandler class implements EventHandler interface. It must implement the following method: public void handle(ActionEvent event) This method is executed in case the color combo box is used to select a color. It should record a choice of the color so that lines can be drawn using it. WidthHandler class (defined as a nested class of DrawingPane class) The ColorHandler class implements EventHandler interface. It must implement the following method: public void handle(ActionEvent event) This method is executed in case the width combo box is used to select a width of line strokes. It should record a choice of the width so that lines can be drawn using it. How to get started: Download the following files and use them as a base of your program: Assignment7.java DrawingPane.java Assignment7.java: import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.StackPane; public class Assignment7 extends Application { public void start(Stage primaryStage) { //create a DrawPane object. See DrawPane.java for details. DrawingPane gui = new DrawingPane(); //put gui on top of the rootPane StackPane rootPane = new StackPane(); rootPane.getChildren().add(gui); // Create a scene and place rootPane in the stage Scene scene = new Scene(rootPane, 600, 400); primaryStage.setTitle("Line Drawing"); primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage } public static void main(String[] args) { Application.launch(args); } } DrawinPane.java: import javafx.scene.control.Button; import javafx.scene.control.ComboBox; import javafx.scene.layout.Pane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.shape.Line; import javafx.scene.paint.Color; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.geometry.Orientation; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent; import java.util.ArrayList; public class DrawingPane extends BorderPane { private Button undoButton, eraseButton; private ComboBox colorCombo, widthCombo; private ArrayList lineList; private Pane canvas; //declare any other necessary instance variables here //---- //Constructor public DrawingPane() { //Step #1: initialize instance variable and set up layout undoButton = new Button("Undo"); eraseButton = new Button("Erase"); undoButton.setMinWidth(80.0); eraseButton.setMinWidth(80.0); //Create the color comboBox and width comboBox, //---- //initialize lineList, it is a data structure we used //to track the lines we created //---- //topPane should contain two combo boxes and two buttons HBox topPane = new HBox(); topPane.setSpacing(40); topPane.setPadding(new Insets(10, 10, 10, 10)); topPane.setStyle("-fx-border-color: black"); //canvas is a Pane where we will draw lines canvas = new Pane(); canvas.setStyle("-fx-background-color: white;"); //add the canvas at the center of the pane and top panel //should have two combo boxes and two buttons this.setCenter(canvas); this.setTop(topPane); //Step #3: Register the source nodes with its handler objects canvas.setOnMousePressed(new MouseHandler()); //---- //---- } //Step #2(A) - MouseHandler private class MouseHandler implements EventHandler { public void handle(MouseEvent event) { //handle MouseEvent here //Note: you can use if(event.getEventType()== MouseEvent.MOUSE_PRESSED) //to check whether the mouse key is pressed, dragged or released //write your own codes here //---- }//end handle() }//end MouseHandler //Step #2(B)- A handler class used to handle events from Undo & Erase buttons private class ButtonHandler implements EventHandler { public void handle(ActionEvent event) { //write your codes here //---- } }//end ButtonHandler //Step #2(C)- A handler class used to handle colors private class ColorHandler implements EventHandler { public void handle(ActionEvent event) { //write your own codes here //---- } }//end ColorHandler //Step #2(D)- A handler class used to handle widths of lines private class WidthHandler implements EventHandler { public void handle(ActionEvent event) { //write your own codes here //---- } }//end WidthHandler }//end class DrawingPane
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
