Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im having trouble understanding why my line drawing program will only draw lines after I make a selection in the width comboBox! Thanks for any

Im having trouble understanding why my line drawing program will only draw lines after I make a selection in the width comboBox! Thanks for any help!

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); } }

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.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.input.MouseEvent;

import java.util.ArrayList; import javafx.collections.FXCollections; import javafx.collections.ObservableList;

public class DrawingPane extends BorderPane { private Button undoButton, eraseButton; private ComboBox colorCombo, widthCombo; private ArrayList lineList; private Pane canvas; public Color currentColor = Color.BLACK; public double lineWidth;

//declare any other necessary instance variables here //---- private Line line = new Line(); //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, //--- ObservableList colorOptions = FXCollections.observableArrayList( // creating color arraylist "Black", "Blue", "Red", "Yellow", "Green" ); colorCombo = new ComboBox(colorOptions); // creating combobox from arraylist colorCombo.getSelectionModel().selectFirst(); // setting default color ObservableList widthOptions = FXCollections.observableArrayList( // creating width arraylist "1", "3", "5", "7" ); widthCombo = new ComboBox(widthOptions); // creating combobox from width arraylist widthCombo.getSelectionModel().selectFirst(); // setting default width lineList = new ArrayList(); //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"); topPane.getChildren().addAll(/*color,*/ colorCombo, /*width,*/ widthCombo, undoButton, eraseButton); // adding elements to Hbox //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()); // register mousehandler with canvas pane canvas.setOnMouseReleased(new MouseHandler()); // register mousehandler with canvas pane undoButton.setOnAction(new ButtonHandler()); // registering undo button with ButtonHandler

eraseButton.setOnAction(new ButtonHandler()); // registering erase button with ButtonHandler colorCombo.setOnAction(new ColorHandler()); // register color comboBox with colorHandler widthCombo.setOnAction(new WidthHandler()); // register widthComboBox with WidthHandler }

//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 //----

if (event.getEventType() == MouseEvent.MOUSE_PRESSED){ double x = event.getX(); double y = event.getY(); line = new Line(); line.setStroke(currentColor); line.setStrokeWidth(lineWidth);

line.setStartX(x); line.setStartY(y); line.setEndX(x); line.setEndY(y); lineList.add(line); canvas.getChildren().clear(); canvas.getChildren().addAll(lineList); } if (event.getEventType() == MouseEvent.MOUSE_RELEASED) { double x2 = event.getX(); double y2 = event.getY();

line.setEndX(x2); line.setEndY(y2); } }//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 //----

if(event.getSource() == eraseButton){ lineList.clear(); canvas.getChildren().clear(); } if (event.getSource() == undoButton){ if (lineList.size() > 0){ lineList.remove(lineList.size() - 1); } canvas.getChildren().clear(); canvas.getChildren().addAll(lineList); } } }//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 //---- String selectedColor = colorCombo.getValue(); if("Black".equals(selectedColor)){ currentColor = Color.BLACK; } if("Blue".equals(selectedColor)){ currentColor = Color.BLUE; } if("Red".equals(selectedColor)){ currentColor = Color.RED; } if("Yellow".equals(selectedColor)){ currentColor = Color.YELLOW; } if("Green".equals(selectedColor)){ currentColor = Color.GREEN; } } }//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 //---

String selectedWidth = widthCombo.getValue();

if("1".equals(selectedWidth)){ lineWidth = 1; //line.setStrokeWidth(lineWidth); } if("3".equals(selectedWidth)){ lineWidth = 3; //line.setStrokeWidth(lineWidth); } if("5".equals(selectedWidth)){ lineWidth = 5; //line.setStrokeWidth(lineWidth); } if("7".equals(selectedWidth)){ lineWidth = 7; // line.setStrokeWidth(lineWidth); } } }//end WidthHandler } //endclass DrawingPane5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions

Question

Clearly differentiate between limited and general powers.

Answered: 1 week ago