Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Painter App Modification) Incorporate the RGBA color chooser you created in the Color Chooser app into the Painter app so that the user can choose

(Painter App Modification) Incorporate the RGBA color chooser you created in the Color Chooser app into the Painter app so that the user can choose any drawing color. Changing a Slider's value should update the color swatch displayed to the user and set the brushColor instance variable to the current Color.

//Ex1303ModifiedPainterApp.java

package ex1303modifiedpainterapp;

import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;

public class Ex1303ModifiedPainterApp extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("Painter.fxml"));

Scene scene = new Scene(root); stage.setTitle("Painter"); // displayed in window's title bar stage.setScene(scene); stage.show(); }

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

//Painter.fxml

//PainterController.java

package ex1303modifiedpainterapp;

import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.scene.shape.Circle;

public class PainterController { // enum representing pen sizes private enum PenSize { SMALL(2), MEDIUM(4), LARGE(6);

private final int radius;

PenSize(int radius) {this.radius = radius;}

public int getRadius() {return radius;} };

// instance variables that refer to GUI components @FXML private RadioButton blackRadioButton; @FXML private RadioButton redRadioButton; @FXML private RadioButton greenRadioButton; @FXML private RadioButton blueRadioButton; @FXML private RadioButton smallRadioButton; @FXML private RadioButton mediumRadioButton; @FXML private RadioButton largeRadioButton; @FXML private Pane drawingAreaPane; @FXML private ToggleGroup colorToggleGroup; @FXML private ToggleGroup sizeToggleGroup;

// instance variables for managing Painter state private PenSize radius = PenSize.MEDIUM; // radius of circle private Paint brushColor = Color.BLACK; // drawing color

// set user data for the RadioButtons public void initialize() { // user data on a control can be any Object blackRadioButton.setUserData(Color.BLACK); redRadioButton.setUserData(Color.RED); greenRadioButton.setUserData(Color.GREEN); blueRadioButton.setUserData(Color.BLUE); smallRadioButton.setUserData(PenSize.SMALL); mediumRadioButton.setUserData(PenSize.MEDIUM); largeRadioButton.setUserData(PenSize.LARGE); }

// handles drawingArea's onMouseDragged MouseEvent @FXML private void drawingAreaMouseDragged(MouseEvent e) { Circle newCircle = new Circle(e.getX(), e.getY(), radius.getRadius(), brushColor); drawingAreaPane.getChildren().add(newCircle); }

// handles color RadioButton's ActionEvents @FXML private void colorRadioButtonSelected(ActionEvent e) { // user data for each color RadioButton is the corresponding Color brushColor = (Color) colorToggleGroup.getSelectedToggle().getUserData(); }

// handles size RadioButton's ActionEvents @FXML private void sizeRadioButtonSelected(ActionEvent e) { // user data for each size RadioButton is the corresponding PenSize radius = (PenSize) sizeToggleGroup.getSelectedToggle().getUserData(); }

// handles Undo Button's ActionEvents @FXML private void undoButtonPressed(ActionEvent event) { int count = drawingAreaPane.getChildren().size();

// if there are any shapes remove the last one added if (count > 0) { drawingAreaPane.getChildren().remove(count - 1); } }

// handles Clear Button's ActionEvents @FXML private void clearButtonPressed(ActionEvent event) { drawingAreaPane.getChildren().clear(); // clear the canvas } }

//ColorChooser.java

// Fig. 13.8: ColorChooser.java // Main application class that loads and displays the ColorChooser's GUI. import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage;

public class ColorChooser extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("ColorChooser.fxml")); Scene scene = new Scene(root); stage.setTitle("Color Chooser"); stage.setScene(scene); stage.show(); }

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

//ColorChooser.fxml

//ColorChooserController

// Fig. 13.9: ColorChooserController.java // Controller for the ColorChooser app import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.fxml.FXML; import javafx.scene.control.Slider; import javafx.scene.control.TextField; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle;

public class ColorChooserController { // instance variables for interacting with GUI components @FXML private Slider redSlider; @FXML private Slider greenSlider; @FXML private Slider blueSlider; @FXML private Slider alphaSlider; @FXML private TextField redTextField; @FXML private TextField greenTextField; @FXML private TextField blueTextField; @FXML private TextField alphaTextField; @FXML private Rectangle colorRectangle;

// instance variables for managing private int red = 0; private int green = 0; private int blue = 0; private double alpha = 1.0; public void initialize() { // bind TextField values to corresponding Slider values redTextField.textProperty().bind( redSlider.valueProperty().asString("%.0f")); greenTextField.textProperty().bind( greenSlider.valueProperty().asString("%.0f")); blueTextField.textProperty().bind( blueSlider.valueProperty().asString("%.0f")); alphaTextField.textProperty().bind( alphaSlider.valueProperty().asString("%.2f")); // listeners that set Rectangle's fill based on Slider changes redSlider.valueProperty().addListener( new ChangeListener() { @Override public void changed(ObservableValue ov, Number oldValue, Number newValue) { red = newValue.intValue(); colorRectangle.setFill(Color.rgb(red, green, blue, alpha)); } } ); greenSlider.valueProperty().addListener( new ChangeListener() { @Override public void changed(ObservableValue ov, Number oldValue, Number newValue) { green = newValue.intValue(); colorRectangle.setFill(Color.rgb(red, green, blue, alpha)); } } ); blueSlider.valueProperty().addListener( new ChangeListener() { @Override public void changed(ObservableValue ov, Number oldValue, Number newValue) { blue = newValue.intValue(); colorRectangle.setFill(Color.rgb(red, green, blue, alpha)); } } ); alphaSlider.valueProperty().addListener( new ChangeListener() { @Override public void changed(ObservableValue ov, Number oldValue, Number newValue) { alpha = newValue.doubleValue(); colorRectangle.setFill(Color.rgb(red, green, blue, alpha)); } } ); } }

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

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

Explain the steps involved in training programmes.

Answered: 1 week ago

Question

What are the need and importance of training ?

Answered: 1 week ago

Question

Describe the job youd like to be doing five years from now.

Answered: 1 week ago

Question

So what disadvantages have you witnessed? (specific)

Answered: 1 week ago