I have this code going to where I have a green box with buttons around it to change the hue, saturation, and make it darker and lighter. I am having trouble with the color changes. if anyone could help this would be greatly appreciated. I need to use the event handler to do so. if anyone can help me with this problem it would be greatly appreciated. This is what I am supposed to do. As I said I'm just having trouble getting the color changes part of it.
ColorPane: extends Pane. It will have one instance variable, a Rectangle.
- The constructor for ColorPane will
- Create the rectangle and set the fill color to something medium: not too bright or too dark, not too saturated or unsaturated.
- Bind the width and height of the rectangle to the width and the height of the pane. That way the rectangle will cover the entire pane
- Set the position of the rectangle to
(0,0)
- Add the rectangle to the pane (it’s not a child just because it is an instance variable)
There will be six methods that change the color of the rectangle. Each of them will follow approximately the same approach:
- Get the Fill of the rectangle and cast it to Color
- Get the hue, saturation, and brightness of the fill color (methods in Color)
- Modify the component that the method is changing
- Recreate the color (Color.hsb → https://docs.oracle.com/javafx/2/api/javafx/scene/paint/Color.html)
- Set the fill of the rectangle
- Each method will change one component of the color in a particular way
- ‘Hue up’ adds 30 to the hue
- ‘Hue down’ subtracts 30 from the hue
- ‘More saturated’ replaces the saturation by its square root1
- ‘Less saturated’ replaces the saturation by its square
- ‘Darker’ replaces the brightness by its square
- ‘Lighter’ replaces the brightness by its square root ShowColors Define an instance variable of type ColorPane in the class. The visual layout should use a border pane as the base. Create panes for the top bottom and right to hold the buttons. Put the ColorPane in the center. You may approach the handlers in any way discussed in the book: inner classes, anonymous classes or lambda expressions.
package application;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ColorSample extends Application {
private ColorPane colorPane = new ColorPane();
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btDarker = new Button("Darker");
Button btBrighter = new Button("Brighter");
hBox.getChildren().add(btDarker);
hBox.getChildren().add(btBrighter);
HBox hBox2 = new HBox();
hBox2.setSpacing(10);
hBox2.setAlignment(Pos.CENTER);
Button btMsat = new Button("More Saturated");
Button btLsat = new Button("Less Saturated");
hBox2.getChildren().add(btMsat);
hBox2.getChildren().add(btLsat);
VBox vBox = new VBox();
vBox.setSpacing(10);
vBox.setAlignment(Pos.CENTER);
Button btHup = new Button("Hue up");
Button btHdown = new Button("Hue down");
vBox.getChildren().add(btHup);
vBox.getChildren().add(btHdown);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(colorPane);
borderPane.setTop(hBox2);
borderPane.setRight(vBox);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
BorderPane.setAlignment(vBox, Pos.CENTER_RIGHT);
BorderPane.setAlignment(hBox2, Pos.TOP_CENTER);
Scene scene = new Scene(borderPane, 600, 600);
primaryStage.setTitle("ColorSample");// Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args) {
launch(args);
}
}
class ColorPane extends StackPane {
private Rectangle r = new Rectangle();
public ColorPane() {
getChildren().add(r);
r.setWidth(520);
r.setHeight(540);
r.setFill(Color.GREEN);
r.setStroke(Color.BLACK);
r.setX(0);
r.setY(0);
}
}