Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You need to adapt the above code to include at least 4 buttons centered along th

ID: 3699637 • Letter: Y

Question

You need to adapt the above code to include at least 4 buttons centered along the top of the window that will allow a user to change the colour of the lines (e.g., red, blue, green, purple). You can have additional buttons if you want. You can use any type of layout to organize this but it must work correctly.

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.scene.input.MouseEvent;

public class ElasticLines extends Application{
    private Line line;
    private Group root;

    @Override
    public void start(Stage primaryStage) {
       
        root = new Group();

        Scene scene = new Scene(root, 500, 500,Color.LIGHTBLUE);
        scene.setOnMousePressed(this::processMousePress);   
        scene.setOnMouseDragged(this::processMouseDrag);
       
        primaryStage.setTitle("Elastic Lines");
        primaryStage.setScene(scene);
        primaryStage.show();
       
    }
    public void processMousePress(MouseEvent e){
       
        line = new Line(e.getX(), e.getY(), e.getX(), e.getY());
        line.setStroke(Color.RED);
        line.setStrokeWidth(3);
        root.getChildren().add(line);
       
    }
    public void processMouseDrag(MouseEvent e){
        line.setEndX(e.getX());
        line.setEndY(e.getY());
       
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
   
}

Here is a sample of what the adapted code will look like:

Elastic Lines RED BLUE GREEN PURPLE

Explanation / Answer

here is your modified code : ---------------->>>>>>>>>>>>

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.event.*;
import javafx.scene.control.Button;
import javafx.scene.Group;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javafx.scene.input.MouseEvent;

public class ElasticLines extends Application{
private Line line;
private Group root;
private Color col = Color.RED;
private Button b1,b2,b3,b4;

@Override
public void start(Stage primaryStage) {

  root = new Group();
  HBox hbox = new HBox();
  b1 = new Button("RED");
  b2 = new Button("BLUE");
  b3 = new Button("GREEN");
  b4 = new Button("PURPLE");
  b1.setOnAction(e -> test(e));
  b2.setOnAction(e -> test(e));
  b3.setOnAction(e -> test(e));
  b4.setOnAction(e -> test(e));
  hbox.setSpacing(30);
  hbox.setPadding(new Insets(0, 20, 10, 100));
  hbox.getChildren().addAll(b1,b2,b3,b4);
  root.getChildren().add(hbox);
  Scene scene = new Scene(root, 500, 500,Color.LIGHTBLUE);
  scene.setOnMousePressed(this::processMousePress);
  scene.setOnMouseDragged(this::processMouseDrag);

  primaryStage.setTitle("Elastic Lines");
  primaryStage.setScene(scene);
  primaryStage.show();
}

public void test(ActionEvent e) {
        if(e.getSource().equals(b2)){
        col = Color.BLUE;
        }
        if(e.getSource().equals(b1)){
        col = Color.RED;
        }
        if(e.getSource().equals(b3)){
        col = Color.GREEN;
        }
        if(e.getSource().equals(b4)){
        col = Color.PURPLE;
        }
    }

public void processMousePress(MouseEvent e){

  line = new Line(e.getX(), e.getY(), e.getX(), e.getY());
  line.setStroke(col);
  line.setStrokeWidth(3);
  root.getChildren().add(line);
}

public void processMouseDrag(MouseEvent e){
  line.setEndX(e.getX());
  line.setEndY(e.getY());
}

public static void main(String[] args) {
  Application.launch(args);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote