简体   繁体   中英

Changing color attributes

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:

    1. Get the Fill of the rectangle and cast it to Color
    2. Get the hue, saturation, and brightness of the fill color (methods in Color)
    3. Modify the component that the method is changing
    4. Recreate the color (Color.hsb → https://docs.oracle.com/javafx/2/api/javafx/scene/paint/Color.html )
    5. 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);
    }

}

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)

To do this, you need to use this code:

Rectangle rectangle = new Rectangle(0, 0, pane.getPrefWidth(), pane.getPrefHeight());
pane.getChildren().add(rectangle);

There will be six methods that change the color of the rectangle. Each of them will follow approximately the same approach:

1: Get the Fill of the rectangle and cast it to Color

2: Get the hue, saturation, and brightness of the fill color (methods in Color)

3: Modify the component that the method is changing

4: Recreate the color (Color.hsb)

5: Set the fill of the rectangle

To get the color, do this:

Color color = (Color) rectangle.getFill();

To get the hue, saturation, and brightness of the color, do this:

double hue = color.getHue();
double saturation = color.getSaturation();
double brightness = color.getBrightness();

To set the fill of the rectangle, do this:

rectangle.setFill(color);

Hue up:

rectangle.setFill(Color.hsb(color.getHue() + 30, color.getSaturation(), color.getBrightness()));

Hue Down:

rectangle.setFill(Color.hsb(color.getHue() - 30, color.getSaturation(), color.getBrightness()));

More Saturated:

rectangle.setFill(Color.hsb(color.getHue(), color.getSaturation()^2, color.getBrightness()));

Less Saturated:

rectangle.setFill(Color.hsb(color.getHue(), Math.sqrt(color.getSaturation()), color.getBrightness()));

Lighter:

rectangle.setFill(Color.hsb(color.getHue(), color.getSaturation(), color.getBrightness()^2));

Darker:

rectangle.setFill(Color.hsb(color.getHue(), color.getSaturation(), Math.sqrt(color.getBrightness())));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM