更改颜色属性 [英] Changing color attributes

查看:96
本文介绍了更改颜色属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这段代码转到有一个绿色框的地方,该框周围有按钮,可以更改色调,饱和度并使它更暗和更亮。我在颜色变化方面遇到麻烦。如果有人可以帮助您,将不胜感激。我需要使用事件处理程序来执行此操作。如果有人可以帮助我解决这个问题,将不胜感激。这就是我应该做的。正如我所说,我只是遇到了部分颜色变化的麻烦。

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:扩展窗格。

ColorPane: extends Pane. It will have one instance variable, a Rectangle.


  • ColorPane的构造函数将是


    • 创建矩形并将填充颜色设置为中等程度:不要太亮或太暗,不要太饱和或不饱和

    • 绑定宽度和高度矩形的宽度和高度。
      这样,矩形将覆盖整个窗格

    • 将矩形的位置设置为(0,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)

      将有六种方法可以更改矩形的颜色。每个人都将遵循大致相同的
      方法:

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


      1. 获取矩形的填充并将其投射为Color

      2. 获取填充颜色的色相,饱和度和亮度(颜色中的方法)

      3. 修改要更改方法的组件

      4. 重新创建颜色(Color.hsb→ https://docs.oracle.com/javafx/2/api/javafx/scene/paint/Color.html

      5. 设置矩形的填充

      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


    • 每种方法都会以一种特定的方式更改颜色的一个分量

    • 色调添加色相30

    • 色相向下从色相中减去30

    • 更饱和用其平方根1代替饱和度

    • 饱和度较低用其平方代替饱和度

    • 深色度用其平方代替其饱和度

    • 较浅 '替代br由其平方根
      确定的颜色ShowColors在类中定义一个ColorPane类型的实例变量。
      视觉布局应使用边框窗格作为基础。为顶部和底部创建窗格以容纳按钮。将
      ColorPane放在中间。您可以采用本书中讨论的任何方式来处理处理程序:内部类,匿名
      类或lambda表达式。

    • 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);
          }
      
      }
      


      推荐答案


      ColorPane的构造函数将
      创建矩形并将填充颜色设置为中等:不是太亮或太暗,也不是太饱和或不饱和。
      将矩形的宽度和高度绑定到窗格的宽度和高度。这样,矩形将覆盖整个窗格
      将矩形的位置设置为(0,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:获取矩形的填充并将其投射为Color

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

      2:获取填充颜色的色相,饱和度和亮度(颜色中的方法)

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

      3:修改要更改方法的组件

      3: Modify the component that the method is changing

      4:重新创建颜色(Color.hsb)

      4: Recreate the color (Color.hsb)

      5:设置矩形的填充

      要获取颜色,请执行以下操作:

      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);
      

      色调:

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

      色调下:

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

      更多饱和状态:

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

      较少饱和:

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

      更轻:

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

      更暗:

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

      这篇关于更改颜色属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆