【发布时间】:2019-08-13 01:09:30
【问题描述】:
我将这段代码转到我有一个绿色框的地方,它周围有一个按钮来更改色调、饱和度,并使其更暗和更亮。我在颜色变化方面遇到了麻烦。如果有人可以帮助这将不胜感激。我需要使用事件处理程序来这样做。如果有人可以帮助我解决这个问题,将不胜感激。这是我应该做的。正如我所说,我只是无法获得颜色变化的一部分。
ColorPane:扩展 Pane。它将有一个实例变量,一个 Rectangle。
- ColorPane 的构造函数将
- 创建矩形并将填充颜色设置为中等:不要太亮或太暗,不要太 饱和或不饱和。
- 将矩形的宽度和高度绑定到窗格的宽度和高度。 这样矩形将覆盖整个窗格
- 设置矩形的位置为
(0,0) - 将矩形添加到窗格中(不是子级,因为它是实例变量)
-
将有六种方法可以改变矩形的颜色。他们每个人都将遵循大致相同的 方法:
- 获取矩形的填充并将其转换为颜色
- 获取填充颜色的色相、饱和度和亮度(Color 中的方法)
- 修改方法正在改变的组件
- 重新创建颜色(Color.hsb → https://docs.oracle.com/javafx/2/api/javafx/scene/paint/Color.html)
- 设置矩形的填充
- 每种方法都会以特定方式更改颜色的一个组成部分
- “Hue up”为色调增加 30
- “Hue down”从色调中减去 30
- “更饱和”将饱和度替换为其平方根1
- “饱和度较低”用平方替换饱和度
- “较暗”用正方形替换亮度
- “Lighter”用平方根替换亮度 ShowColors 在类中定义一个 ColorPane 类型的实例变量。 视觉布局应使用边框窗格作为基础。为顶部底部和右侧创建窗格以按住按钮。放 中心的 ColorPane。你可以用本书中讨论的任何方式来处理处理程序:内部类、匿名 类或 lambda 表达式。
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);
}
}
【问题讨论】:
-
您选择了哪种处理程序方法?注意
ColorPane extends Pane. -
Lambda 表达式。
-
优秀;请edit您的问题显示您修改后的minimal reproducible example;我发现更容易专注于一个控件,比如
btHup,以及它对应的ColorPane方法;请注意,为方便起见,ColorPane可以嵌套,正如 here 所建议的那样。
标签: javafx colors event-handling