Question: / / Main.java import javafx.application.Application; import javafx.application.Platform; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle;

// Main.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class Main extends Application {
private Graph graph;
@Override
public void start(Stage primaryStage){
graph = new Graph();
BorderPane root = new BorderPane();
Scene scene = new Scene(root,600,600);
GraphPane graphPane = new GraphPane();
root.setCenter(graphPane);
HBox controls = new HBox(10);
controls.setStyle("-fx-padding: 10px;");
root.setBottom(controls);
TextField vertex1Field = new TextField();
TextField vertex2Field = new TextField();
Button addEdgeButton = new Button("Add Edge");
addEdgeButton.setOnAction(e -> addEdge(vertex1Field.getText(), vertex2Field.getText()));
controls.getChildren().addAll(addEdgeButton, new Label("Vertex 1:"), vertex1Field,
new Label("Vertex 2:"), vertex2Field);
Button isConnectedButton = new Button("Is Connected?");
isConnectedButton.setOnAction(e -> checkConnected());
Button hasCyclesButton = new Button("Has Cycles?");
hasCyclesButton.setOnAction(e -> checkCycles());
Button depthFirstButton = new Button("Depth First Search");
depthFirstButton.setOnAction(e -> performDepthFirstSearch());
Button breadthFirstButton = new Button("Breadth First Search");
breadthFirstButton.setOnAction(e -> performBreadthFirstSearch());
controls.getChildren().addAll(isConnectedButton, hasCyclesButton, depthFirstButton, breadthFirstButton);
primaryStage.setScene(scene);
primaryStage.setTitle("Graph Component");
primaryStage.show();
}
private void addEdge(String vertex1, String vertex2){
// Assume vertex coordinates are predefined for simplicity
double x1=100, y1=100;
double x2=200, y2=200;
if (!vertex1.isEmpty() && !vertex2.isEmpty()){
graph.addEdge(vertex1, vertex2);
drawEdge(x1, y1, x2, y2);
}
}
private void drawEdge(double x1, double y1, double x2, double y2){
Line line = new Line(x1, y1, x2, y2);
line.setStrokeWidth(2);
line.setStroke(Color.BLACK);
GraphPane graphPane =(GraphPane)((BorderPane) graphPane.getScene().getRoot()).getCenter();
graphPane.getChildren().add(line);
}
private void checkConnected(){
boolean isConnected = graph.isConnected();
displayMessage(isConnected ? "The graph is connected." : "The graph is not connected.");
}
private void checkCycles(){
boolean hasCycles = graph.hasCycles();
displayMessage(hasCycles ? "The graph has cycles." : "The graph does not have cycles.");
}
private void performDepthFirstSearch(){
List dfs = graph.depthFirstSearch();
displayMessage("Depth First Search: "+ dfs.toString());
}
private void performBreadthFirstSearch(){
List bfs = graph.breadthFirstSearch();
displayMessage("Breadth First Search: "+ bfs.toString());
}
private void displayMessage(String message){
TextField messageField =(TextField)((BorderPane) graphPane.getScene().getRoot()).getBottom();
Platform.runLater(null);
messageField.setText(message);
}
public static void main(String[] args){
launch(args);
}
}import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import java.util.HashMap;
import java.util.Map;
public class GraphPane extends Pane {
private final Map vertexCircles = new HashMap<>();
private char nextVertexLabel ='A';
public GraphPane(){
setOnMouseClicked(event ->{
double x = event.getX();
double y = event.getY();
addVertex(x, y);
});
}
public void addVertex(double x, double y){
if (nextVertexLabel >'Z'){
return; // Limit to 26 vertices
}
String vertexName = String.valueOf(nextVertexLabel++);
Circle circle = new Circle(x, y,15, Color.LIGHTBLUE);
Text label = new Text(x -5, y +5, vertexName);
vertexCircles.put(vertexName, circle);
getChildren().addAll(circle, label);
}
public void drawEdge(String vertex1, String vertex2){
Circle circle1= vertexCircles.get(vertex1);
Circle circle2= vertexCircles.get(vertex2);
if (circle1!= null && circle2!= null){
Line line = new Line(circle1.getCenterX(), circle1.getCenterY(),
circle2.getCenterX(), circle2.getCenterY());
line.setStroke(Color.BLACK);
getChildren().add(line);
}
}
}//Need help implemening message textfield, making sure the buttons work, and adding edges to the vertices.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!