在 JavaFX 中对图像使用 ColorPicker

Use of ColorPicker on an image in JavaFX

提问人:zaman 提问时间:7/6/2023 最后编辑:jewelseazaman 更新时间:7/7/2023 访问量:79

问:

我在imageview中有一个图像。我想做的是给它添加颜色。基本上,它就像图像上的一层颜色。所以我正在尝试使用 ColorPicker 为 T 恤图像添加颜色,但我不知道该怎么做。我已经尝试过它与 colorAdjust 类一起尝试过,但它在 T 恤上给我的颜色与我从 ColorPicker 中选择的颜色不同。

顺便说一句,我正在使用 SceneBuilder。

图形用户界面

GUI

体恤衫

T-shirt

@FXML
private void changecolor(ActionEvent event) {      
    Color mycolor=mycolorpicker.getValue();
    label.setBackground(new Background(new BackgroundFill(mycolor,null,null)));
    Image img=new Image(getClass().getResourceAsStream("tshirt7.PNG"));
    imageV.setImage(img);
    ColorAdjust colorAdjust = new ColorAdjust();
    colorAdjust.setHue((mycolor.getHue()/360));
    colorAdjust.setSaturation(mycolor.getSaturation());
    colorAdjust.setBrightness(mycolor.getBrightness()-1);
   
    imageV.setEffect(colorAdjust);
}

我期望图像上的颜色与我从 ColorPicker 中选择的颜色相同。但是T恤上的颜色并不准确。

Java 图像 javafx imageview 颜色选择器

评论

0赞 geanakuch 7/6/2023
请不要把代码的图片,把代码作为文字粘贴到问题中。
0赞 jewelsea 7/6/2023
但请务必附上复制问题所需的图片(原始 T 恤👚图片)。

答:

2赞 jewelsea 7/7/2023 #1

如何从拾色器为图像着色。

有关详细信息,请参阅:

将使用的图像(我使用了您的问题中提供的图像)放在与放置示例代码的包具有相同目录结构的资源文件夹中。

purple

orange

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.effect.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

import java.util.Objects;

public class TShirtDesigner extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        ColorPicker colorPicker = new ColorPicker();

        Image tshirtImage = new Image(
                Objects.requireNonNull(
                        TShirtDesigner.class.getResource(
                                "tshirt.png"
                        )
                ).toExternalForm()
        );

        ImageView tshirt = new ImageView(tshirtImage);
        tshirt.setClip(new ImageView(tshirtImage));

        bindImageColor(
                tshirt,
                colorPicker.valueProperty()
        );

        VBox layout = new VBox(
                10,
                colorPicker,
                tshirt
        );
        layout.setAlignment(Pos.CENTER);
        layout.setPadding(new Insets(10));

        Scene scene = new Scene(layout);
        stage.setScene(scene);
        stage.show();
    }

    private static void bindImageColor(
            ImageView imageView,
            ObjectProperty<Color> colorProperty
    ) {
        ColorInput colorInput = new ColorInput(
                0,
                0,
                imageView.getImage().getWidth(),
                imageView.getImage().getHeight(),
                colorProperty.get()
        );
        colorInput.paintProperty().bind(
                colorProperty
        );

        ColorAdjust monochrome = new ColorAdjust();
        monochrome.setSaturation(-1.0);

        Blend colorBlend = new Blend(
                BlendMode.MULTIPLY,
                monochrome,
                colorInput
        );

        imageView.setEffect(colorBlend);
    }

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

评论

0赞 zaman 7/8/2023
非常感谢@jewelsea。成功了!!你对我帮了个大忙。我被困在这里 4 天。我不确定如何回复这个平台中的用户,因为我是新用户,但我希望它能到达你!